Automatic type conversion for numbers and strings
When you use -
, *
, or /
on any inputs, the output will be of type number (possibly NaN
). Notice that null
and false
are assigned the numerical value 0 and true
is assigned the numerical value 1, but that calculating with undefined
results in NaN
. Similarly, notice how "5"
is interpreted as the integer 5
and "3.4"
as the floating point number 3.4
, but a calculation using "dog"
results in NaN
.
Be very careful of +
, which has meanings for both numbers and strings. If at least one of the inputs is a string, the output will be a string. Notice that this also applies to the shorthand +=
; if you use n += "string"
for a number n
, n
is converted into a string.
Note:
Hints:
- undefined
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
2
var num = 6;
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX