The JavaScriptDocumentation 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 functions as both a unary operator for numeric type conversion and a binary operator for numeric addition and string concatenation. Its execution behavior is strictly governed by ECMAScript’s implicit type coercion rules, specifically relying on the ToPrimitive, ToString, and ToNumeric internal abstract operations.
Unary Plus (+x)
The unary plus operator precedes a single operand. It evaluates the operand and applies the internal ToNumber operation.
valueOf(), and if that does not return a primitive, it calls toString(), before finally applying ToNumber.
Binary Plus (x + y)
The binary plus operator requires two operands. It performs either string concatenation or numeric addition based on the types of the evaluated operands after primitive coercion.
The Evaluation Algorithm
When the engine encountersx + y, it executes the following sequence:
- Evaluate Operands: Both the left and right expressions are evaluated.
- ToPrimitive Conversion: The engine applies ToPrimitive(operand, default) to both values.
- For most objects, the
defaulthint behaves like theNumberhint (invokingvalueOf()thentoString()). - Exception:
Dateobjects treat thedefaulthint asString(invokingtoString()thenvalueOf()).
- For most objects, the
- String Check: If either of the resulting primitive values is a
String, the engine applies ToString to both operands and performs string concatenation. - Numeric Addition: If neither primitive is a
String, the engine applies ToNumeric to both operands and performs mathematical addition.
Syntax Visualization: Numeric Addition
Triggered when neither resolved primitive is a String.Syntax Visualization: String Concatenation
Triggered when at least one resolved primitive is a String.Syntax Visualization: Object Coercion
Demonstrating the ToPrimitive step before the String/Numeric check.Type-Specific Edge Cases
- BigInt: The binary
+operator supportsBigIntoperands, but mixingBigIntandNumberthrows aTypeErrorbecause the specification explicitly forbids implicit coercion between these two numeric types to prevent precision loss.
Master JavaScript with Deep Grasping Methodology!Learn More





