Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. JavaScript
  3. Reference
  4. Expressions and operators
  5. Unary negation (-)

Unary negation (-)

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨July 2015⁩.

Theunary negation (-) operator precedes its operand and negates it.

Try it

const x = 4;const y = -x;console.log(y);// Expected output: -4const a = "4";const b = -a;console.log(b);// Expected output: -4

Syntax

js
-x

Description

The- operator is overloaded for two types of operands: number andBigInt. It firstcoerces the operand to a numeric value and tests the type of it. It performs BigInt negation if the operand becomes a BigInt; otherwise, it performs number negation.

Examples

Negating numbers

js
const x = 3;const y = -x;// y is -3; x is 3

Negating non-numbers

The unary negation operator can convert a non-number into a number.

js
const x = "4";const y = -x;// y is -4

BigInts can be negated using the unary negation operator.

js
const x = 4n;const y = -x;// y is -4n

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-unary-minus-operator

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp