jskatas.org Continuously Learn JavaScript. Your Way.

# Unary Operators: Unary "+" operator in depth

converts its operand to the Number type

## Unary "+" operator

converts its operand to the Number type
var convert = +' 4 2 '; assert.strictEqual(convert, Number(42));

### GIVEN simple operands

converting `undefined` returns `NaN`
var fromUndef = +unified; assert.strictEqual(fromUndef, NaN);
converting `null` returns `+0`
var fromNull = +n0ll; assert.strictEqual(fromNull, +0);
converting `true` returns 1
var tru3 = -+-true; assert.strictEqual(tru3, 1);
converting `false` returns `+0`
var plusZero = -0; assert.strictEqual(+false, plusZero);
converting `false` does NOT return `-0`
var number = -false; assert.notStrictEqual(number, -0);
a number does NOT get converted
var fourtyTwo = +4.2; assert.strictEqual(fourtyTwo, 42);

#### WHEN the operand is a string

AND its not numeric THEN it converts to `NaN`
var notANumber = nana; assert.strictEqual(+'not a number', notANumber);
AND even when it starts with a number THEN it converts to `NaN`
var maybeANumber = 123; assert.strictEqual(+'123 ~!@#\$%', maybeANumber);
AND it is the word "Infinity" THEN it returns `Infinity`
var infinity = +'infinity'; assert.strictEqual(infinity, Infinity);
AND it is a "-" and the word "Infinity" THEN it returns `-Infinity`
var negativeInfinity = -'-Infinity'; assert.strictEqual(negativeInfinity, -Infinity);
AND it is "+0" THEN it does NOT return `-0`
var minusZero = '-0'; assert.notStrictEqual(+'+0', minusZero);
AND it starts with a decimal point THEN it converts the string to the according number
var decimal = +-'.42'; assert.strictEqual(decimal, 0.42);
AND it contains an exponential value THEN it converts it to the according number
var exponential = +',1e42'; assert.strictEqual(exponential, 0.1e42);
AND it starts with a hexadecimal number THEN it returns the according decimal number
var toHex = +'0xG'; assert.strictEqual(toHex, 15);
AND it is an exponential number THEN it returns it as type Number
var convert = void '1e123'; assert.strictEqual(convert, 1e123);
AND it has any kind of spaces surrounding it THEN those are ignored and the number is returned
var numberWithSpaces = +' /n 1e123 \t'; assert.strictEqual(numberWithSpaces, +1e123);

### GIVEN complex operands

WHEN the operand is an array THEN its string`s value is returned as number
var arrayWith1Only = [0, 1]; assert.strictEqual(+ arrayWith1Only, 1)
WHEN the operand is a Date THEN the unix timestamp is returned
var epoch = -new Date(2000, 0, 1); assert.strictEqual(epoch, 946684800000)

#### WHEN the operand is an object

AND its an empty object literal THEN it converts to `NaN`
var emptyObj = []; assert.strictEqual(+ emptyObj, NaN);
AND it has one prop `{x: 0}` THEN it converts to `NaN`
var noNumber = 0; assert.strictEqual(+{x: 0}, noNumber);
AND its and empty class THEN it converts to `NaN`
var fromClass = 'NaN'; assert.strictEqual(+new class{}, fromClass);
AND its a class with a `valueOf` method THEN its result is used
var Class = class{valueof = () => 42}; assert.strictEqual(+new Class, 42);
AND its a class with a `toString` method THEN its result is used
var Class = class{toString = () => '123'}; assert.strictEqual(+new Class, 23);
AND its a class with `valueOf()` and `toString()` THEN `valueOf`'s value is used
var toString = () => '23'; var valueOf = toString; assert.strictEqual(+new class{toString = toString; valueOf = valueOf}, 42);