JavaScript Unary Plus and Negation Operators
JavaScript provides unary plus (+) and unary negation
(-) operators to perform arithmetic evaluation and type
coercion on a single operand. Both operators convert their operand into
a numeric primitive using the abstract ToNumber operation
defined in the ECMAScript specification. While the unary plus returns
the converted numeric value as-is, the unary negation converts the
operand to a number and then reverses its sign.
The Unary Plus Operator
(+)
The unary plus operator precedes its operand and evaluates to the operand’s numeric representation. It serves as the fastest and most concise way to explicitly cast a value to a number.
- Numbers: If the operand is already a number, its
value remains unchanged (
+42evaluates to42). - Strings: Numeric strings are parsed into numbers
(
+"123"becomes123,+"3.14"becomes3.14). An empty string or whitespace string evaluates to0(+""becomes0). Non-numeric strings evaluate toNaN(+"hello"becomesNaN). - Booleans:
+trueevaluates to1, and+falseevaluates to0. - Null and Undefined:
+nullevaluates to0, while+undefinedevaluates toNaN. - Symbols and BigInts: Applying unary plus to a
Symbolor aBigIntthrows aTypeError.
+42; // 42
+"100"; // 100
+""; // 0
+true; // 1
+false; // 0
+null; // 0
+undefined; // NaN
+"text"; // NaNThe Unary Negation Operator
(-)
The unary negation operator precedes its operand, converts the operand into a number if necessary, and then negates the resulting value.
- Numbers: Inverts the sign of the number
(
-5evaluates to-5,-(-5)evaluates to5). Applying negation to0results in-0. - Strings, Booleans, and Null: Values undergo the
same numeric conversion as unary plus before having their sign inverted
(
-"42"becomes-42,-truebecomes-1,-nullbecomes-0). - BigInts: Unlike the unary plus, the unary negation
operator supports
BigIntvalues (-10nevaluates to-10n). - Symbols and Undefined: Symbols throw a
TypeError, while-undefinedevaluates toNaN.
-42; // -42
-"50"; // -50
-true; // -1
-false; // -0
-null; // -0
-undefined; // NaN
-10n; // -10nEvaluating Objects and Non-Primitive Values
When either operator encounters an object, JavaScript first converts
the object into a primitive value using the internal
ToPrimitive algorithm with a number hint.
- Method Resolution: The engine checks for a
[Symbol.toPrimitive]('number')method. If absent, it attempts to call the object’svalueOf()method, followed bytoString(). - Numeric Coercion: Once a primitive value (string,
boolean, or number) is returned, the engine applies the standard
ToNumbercoercion followed by the operator’s logic.
// Empty array converts to empty string "", then to 0
+[]; // 0
-[]; // -0
// Single-element array converts to "5", then to 5
+[5]; // 5
-[5]; // -5
// Multi-element array converts to "1,2", which is not a valid number
+[1, 2]; // NaN
// Plain object converts to "[object Object]", which is not a valid number
+{}; // NaN
// Date objects convert to their millisecond Unix timestamp via valueOf()
+new Date("2026-01-01"); // 1767225600000