Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The + operator in JavaScript is a polymorphic operator that performs either numeric addition or string concatenation depending on the types of its evaluated operands. It also functions as a unary operator to explicitly coerce a single operand into a numeric value.

Syntax

// Binary Operator
LeftOperand + RightOperand

// Unary Operator
+Operand

Binary Operator Mechanics

When used as a binary operator, the JavaScript engine follows a strict evaluation algorithm defined by the ECMAScript specification to determine whether to add or concatenate. The engine resolves the operation through the following sequence:
  1. Primitive Conversion: The engine applies the internal ToPrimitive() abstract operation to both operands. Objects and Arrays are converted to their primitive values (typically by invoking their internal valueOf() or toString() methods).
  2. Type Evaluation:
    • If either of the resulting primitive values is a String, the engine applies the ToString() abstract operation to both operands and performs string concatenation.
    • If neither primitive value is a String, the engine applies the ToNumeric() abstract operation to both operands and performs numeric addition. If the operands resolve to different numeric types (e.g., mixing Number and BigInt), the engine throws a TypeError.
    • If either operand is a Symbol, the engine throws a TypeError as Symbols cannot be implicitly coerced to strings or numbers.

Coercion Behavior

// Numeric Addition (neither operand resolves to a string)
1 + 2               // 3
1 + true            // 2 (true coerces to 1)
1 + null            // 1 (null coerces to 0)
1 + undefined       // NaN (undefined coerces to NaN)

// String Concatenation (at least one operand resolves to a string)
"1" + 2             // "12"
"System" + true     // "Systemtrue"
"" + null           // "null"

// Complex Type Coercion (ToPrimitive evaluation)
[1, 2] + [3, 4]     // "1,23,4" (Arrays coerce to strings "1,2" and "3,4")
({} + [])           // "[object Object]" (Expression context forces Object coercion)

// BigInt and Symbol Exceptions
10n + 20n           // 30n
10n + 5             // TypeError: Cannot mix BigInt and other types
Symbol("a") + "b"   // TypeError: Cannot convert a Symbol value to a string

Unary Operator Mechanics

When used as a prefix to a single operand, the unary + operator directly triggers the internal ToNumber() abstract operation. It bypasses string concatenation logic entirely and attempts to parse the operand into an IEEE 754 double-precision float. Because the operation strictly enforces ToNumber(), applying the unary + to a BigInt or Symbol will throw a TypeError.

Unary Coercion Behavior

+"42"               // 42 (String to Number)
+"0xFF"             // 255 (Hexadecimal string to Number)
+true               // 1 (Boolean to Number)
+false              // 0
+null               // 0
+undefined          // NaN
+""                 // 0 (Empty string to Number)
+new Date()         // 1697040000000 (Date object to epoch timestamp)
+{}                 // NaN (Object to Number)

// BigInt and Symbol Exceptions
+10n                // TypeError: Cannot convert a BigInt value to a number
+Symbol("a")        // TypeError: Cannot convert a Symbol value to a number
Master JavaScript with Deep Grasping Methodology!Learn More