There are three primitive data types in JavaScript: string, number and boolean.
Anything can be converted to a string or boolean:
- All objects convert to
true
(exceptnull
, which becomesfalse
- I only mention it here becausetypeof null
givesobject
) - All objects have a built-in
toString
method which is called when converting to a string. - Converting a number to a string is done by giving the string representation of the number (ie.
5
becomes"5"
) - Numbers convert to boolean
true
, unless it's0
which becomesfalse
.
Converting to a number is a little trickier, but technically possible. If it can find a valid number, then it becomes that number. Otherwise, it becomes NaN
.
So basically... any type can become any other type through casting in this way. The only time you have anything resembling an "error condition" is NaN
.