Why does the JavaScript expression ++[[]][+[]]+[+[]] evaluate to the string “10”? Can you explain the step-by-step evaluation process?
The JavaScript expression ++[[]][+[]]+[+[]] evaluates to the string “10” due to JavaScript’s type conversion rules and operator precedence. This seemingly complex expression follows a specific step-by-step evaluation process.
Contents
- Breaking Down the Expression
- Step-by-Step Evaluation Process
- Type Conversion Rules
- Operator Precedence
- Practical Implications
Breaking Down the Expression
The expression ++[[]][+[]]+[+[]] can be divided into three main parts:
++[[]][+[]]- The first operand+- The addition/concatenation operator[+[]]- The second operand
Understanding how each part works is key to understanding the final result.
Step-by-Step Evaluation Process
Part 1: Evaluating [[]]
The expression [[]] creates an array containing one element, which is an empty array:
[[]] // → [ [] ]
Part 2: Evaluating +[]
The unary plus operator + converts its operand to a number. When applied to an empty array:
+[] // → 0
According to the MDN documentation, the unary plus operator “precedes its operand and evaluates to its operand but attempts to convert it into a number, if it isn’t already.”
Part 3: Array Indexing [[]][+[]]
Now we have array indexing:
[[]][+[]] // → [][0] → []
The array [[]] has one element at index 0 (the empty array []), so this returns the empty array.
Part 4: Increment Operator ++[[]][+[]]
The increment operator is applied to the result:
++[[]][+[]] // → ++[] → ++0 → 1
The increment operator converts the empty array to a number (0), then increments it by 1, resulting in the number 1.
Part 5: Evaluating [+[]] Again
The second part of the expression is another array:
[+[]] // → [0]
This creates an array containing the number 0.
Type Conversion Rules
The final step involves the addition operator and type conversion:
1 + [0] // → 1 + "0" → "10"
When the addition operator encounters different types, JavaScript applies specific type conversion rules:
- Both operands are first converted to primitives
- If either primitive value is a string, string concatenation is performed
- Otherwise, numeric addition is performed
In this case:
- The number
1is converted to the string"1" - The array
[0]is converted to the string"0"(via itstoString()method) - String concatenation produces
"10"
Operator Precedence
The order of operations follows JavaScript’s operator precedence rules:
- Array indexing (
[]) has higher precedence than unary operators - Unary plus (
+) is evaluated before increment (++) - Increment (
++) has higher precedence than addition (+)
This is why the expression is evaluated as ++([[]][+[]]) + [+[]] rather than other possible groupings.
Practical Implications
This expression demonstrates several important JavaScript concepts:
- Type coercion: JavaScript automatically converts between types
- Array behavior: Empty arrays convert to
0in numeric contexts - String concatenation: When addition involves strings, concatenation occurs
- Implicit conversion: Objects and arrays have default string representations
While this expression is primarily a curiosity, understanding these concepts helps avoid unexpected behavior in real code. For example:
// This produces "10", not 11
console.log(1 + [0]); // "10"
// This produces "10", not 2
console.log([1] + [0]); // "10"
The expression ++[[]][+[]]+[+[]] is a great example of how JavaScript’s type conversion rules can produce surprising results when different types are combined.
Sources
- How does the following piece of expression evaluates to “10” - Stack Overflow
- Why does ++[[]][+[]]+[+[]] evaluates to 10 - Stack Overflow
- Why ++[[]][+[]]+[+[]] evaluates to “10” in JavaScript
- Unary plus (+) - JavaScript | MDN
- Expressions and operators - JavaScript | MDN
- JavaScript Unary Operators - JavaScript Tutorial
Conclusion
The expression ++[[]][+[]]+[+[]] evaluates to the string “10” through a series of type conversions:
[[]]creates an array containing an empty array+[]converts the empty array to the number0[[]][+[]]indexes the array to get the empty array++[[]][+[]]increments the empty array (converting it to0first, then to1)[+[]]creates an array containing the number0- The addition operator performs string concatenation, resulting in
"10"
This demonstrates JavaScript’s flexible type conversion system and how different operators interact with various data types. While such expressions are rarely used in practical code, understanding these concepts helps prevent unexpected behavior in everyday JavaScript programming.