TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
- operator in JavaScript functions as both a binary subtraction operator and a unary negation operator, depending on its execution context (arity). In both forms, it implicitly coerces non-numeric operands to numeric primitives (Number or BigInt) via the abstract ToNumeric operation before evaluation.
Binary Subtraction Operator
As a binary operator,- computes the mathematical difference between two operands.
- Both
leftOperandandrightOperandare evaluated. - Both values are passed through the abstract
ToNumericoperation. - If the types of the two resulting primitives differ (e.g., one is a
Numberand the other is aBigInt), aTypeErroris thrown. - The right numeric value is subtracted from the left numeric value according to IEEE 754 double-precision floating-point arithmetic (or BigInt arithmetic).
Unary Negation Operator
As a unary operator,- precedes a single operand. It coerces the operand to a numeric type and inverts its algebraic sign.
- The
operandis evaluated. - The value is passed through the abstract
ToNumericoperation. - If the resulting value is
NaN, the result remainsNaN. - Otherwise, the sign of the numeric value is inverted.
- Edge Case for Zero: For
Numbertypes, negating0yields-0(JavaScript distinguishes between+0and-0). However,BigIntdoes not possess a negative zero concept; negating0nevaluates strictly to0n.
Implicit Type Coercion (ToNumeric)
Because the - operator is strictly mathematical, it forces type coercion on non-numeric types. When applying the - operator to non-numeric primitives, the JavaScript engine applies ToNumeric, which delegates to ToNumber for standard primitives:
Object Coercion
When an operand is an Object, JavaScript attempts to convert it to a primitive value using the abstractToPrimitive operation, prioritizing the number hint.
The resolution follows strict rules:
- If the object defines a
[Symbol.toPrimitive]method, it is called exclusively with the"number"hint. If this method returns an object instead of a primitive, aTypeErroris thrown immediately (it does not fall back to other methods). - If
[Symbol.toPrimitive]is absent, the engine falls back to callingvalueOf(). IfvalueOf()returns a primitive, that value is used. - If
valueOf()is absent or returns an object, the engine falls back to callingtoString().
ToNumeric. If the returned primitive is a BigInt, ToNumeric returns it directly. If the primitive is any other type, it is subjected to ToNumber coercion rules.
IEEE 754 Edge Cases
Because JavaScript Numbers are double-precision floats, the- operator adheres to specific IEEE 754 rules regarding Infinity, -Infinity, and NaN:
Master JavaScript with Deep Grasping Methodology!Learn More





