Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Unary minus

Unary minus (`unary minus`), written as `-expression`, converts its operand to a numeric value and returns its negation. It supports both `Number` and `BigInt`; it does not assign the result back to the variable.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

-expression

The unary minus operator is written as -expression. It takes one operand, unlike subtraction a - b, which takes two. It returns the opposite value: -7 produces a negative number, while -(-7) produces a positive one. In -value, the operator does not assign the result to value, but if value holds an object, code run during its conversion can reassign value.

Conversion with ToNumeric happens first. A Number stays a Number, and a BigInt stays a BigInt: -12n produces -12n. The string "12" is converted to a number, so -"12" produces -12. The string "hello" produces NaN, whereas a Symbol operand causes a TypeError. Converting an object can run its own code and throw an exception.

For Number, the operator changes +0 to -0 and vice versa; negating NaN still produces NaN. Negative zero matters: 1 / -0 produces -Infinity. BigInt has no separate negative zero, so -0n produces 0n.

The example demonstrates negation without assignment, string conversion, BigInt handling, negative zero, and NaN. Calling String(-12n) makes the displayed BigInt result consistent.

Examples

const value = 7;
console.log(-value);
console.log(value);
console.log(-"12");
console.log(String(-12n));
console.log(1 / -0);
console.log(Number.isNaN(-"hello"));

Result

-7
7
-12
-12
-Infinity
true

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources