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
Required Knowledge
- All unary operators (Unary Operators)
- Unary "+" operator (Unary Operators)
Related Katas
Unary Operators
- All unary operators
- Unary "+" operator
- Unary "+" operator in depth
Bitwise Shift Operators
Rest operator
Spread operator
Difficulty Level
ADVANCED
First Published
7 February 2021
Stats
25 tests to solve