Unary Operators: Unary "+" operator, in depth
converts its operand to the Number type
Donate to NGO Julenka. Support Ukranians in need.
Julenka is an NGO which my brother founded in 2011 to support Ukranian families and kids in need.
Unary "+" operator
- converts its operand to the Number type
GIVEN simple operands
- converting
undefined
returns NaN
- converting
null
returns +0
- converting
true
returns 1
- converting
false
returns +0
- converting
false
does NOT return -0
- a number does NOT get converted
WHEN the operand is a string
- AND its not numeric THEN it converts to
NaN
- AND even when it starts with a number THEN it converts to
NaN
- AND it is the word "Infinity" THEN it returns
Infinity
- AND it is a "-" and the word "Infinity" THEN it returns
-Infinity
- AND it is "+0" THEN it does NOT return
-0
- AND it starts with a decimal point THEN it converts the string to the according number
- AND it contains an exponential value THEN it converts it to the according number
- AND it starts with a hexadecimal number THEN it returns the according decimal number
- AND it is an exponential number THEN it returns it as type Number
- AND it has any kind of spaces surrounding it THEN those are ignored and the number is returned
GIVEN complex operands
- WHEN the operand is an array THEN its string`s value is returned as number
- WHEN the operand is a Date THEN the unix timestamp is returned
WHEN the operand is an object
- AND its an empty object literal THEN it converts to
NaN
- AND it has one prop
{x: 0}
THEN it converts to NaN
- AND its and empty class THEN it converts to
NaN
- AND its a class with a
valueOf
method THEN its result is used
- AND its a class with a
toString
method THEN its result is used
- AND its a class with
valueOf()
and toString()
THEN valueOf
s value is used