The unary plus operator

suggest change

The unary plus (+) converts its operand with ToNumber. A BigInt operand throws TypeError instead of becoming a number.

Syntax:

+expression

Returns:

Description

It can convert:

Values that can’t be converted will evaluate to NaN.

Examples:

+42           // 42
+"42"         // 42
+true         // 1
+false        // 0
+null         // 0
+undefined    // NaN
+NaN          // NaN
+"foo"        // NaN
+{}           // NaN
+function(){} // NaN

Note that attempting to convert an array can result in unexpected return values.

In the background, arrays are first converted to their string representations:

[].toString() === '';
[1].toString() === '1';
[1, 2].toString() === '1,2';

The operator then attempts to convert those strings to numbers:

+[]           // 0   ( === +'' )
+[1]          // 1   ( === +'1' )
+[1, 2]       // NaN ( === +'1,2' )

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents