The unary negation operator (-)

suggest change

The unary negation (-) precedes its operand and negates it, after trying to convert it to number.

Syntax:

-expression

Returns:

Description

The unary negation (-) can convert the same types / values as the unary plus (+) operator can.

Values that can’t be converted will evaluate to NaN (there is no -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:


Unary Operators:
* Syntax
* The unary negation operator (-)

Table Of Contents
11 Arrays
12 Objects
14 Classes
16 Map
17 Set
21 Unary Operators
24 Loops
27 Date
29 Scope
30 AJAX
35 Cookies
41 JSON
44 Fetch
45 Modules
46 Screen
64 Console
68 Symbols
73 Modals
76 Events
86 Proxy
89 WeakMap
90 WeakSet
102 Tilde