Question
In JavaScript, the following expression is valid and returns the string "10":
console.log(++[[]][+[]] + [+[]])
Why does this happen? What steps does JavaScript follow to evaluate this expression, and how do array access, unary +, prefix increment ++, and type coercion combine to produce the final string result?
Short Answer
By the end of this page, you will understand how JavaScript evaluates ++[[]][+[]] + [+[]] step by step. You will learn how empty arrays are converted to numbers and strings, how +[] becomes 0, how [[]][0] resolves to an empty array, how prefix increment triggers numeric coercion, and why the final + performs string concatenation instead of numeric addition.
Concept
JavaScript allows many implicit type conversions. This is called type coercion. The expression
++[[]][+[]] + [+[]]
looks strange, but it is built from a few normal rules:
+valuetries to convertvalueto a number.array[index]accesses an element in an array.++valueconvertsvalueto a number, adds1, and returns the incremented value.a + bdoes either numeric addition or string concatenation, depending on the types after conversion.
The reason this matters is that JavaScript code often interacts with values from forms, APIs, query strings, configuration files, and user input. Those values may not already be in the type you expect. Understanding coercion helps you read unusual code, debug bugs, and avoid surprising behavior.
In this expression, the final result is "10" because:
+[]becomes0.[[]][0]gives[].++[]converts[]to , increments it to .
Mental Model
Think of JavaScript like a cashier who keeps converting items into a format it can work with.
+[]is like asking, "Can this empty array be treated as a number?" JavaScript says: yes, that becomes0.[[]][0]is like opening a box and taking out the first item. The outer array contains one item: an empty array.++is like saying, "Turn this into a number and add one."- The final
+is tricky: if one side turns into a string, JavaScript switches from math mode to joining text mode.
So the expression is basically:
- get an empty array
- turn it into
0 - increment it to
1 - take another array that becomes
"0" - join
1and"0" - result:
"10"
A useful shortcut is: arrays often become strings first, and strings strongly influence the + operator.
Syntax and Examples
Here are the key pieces of syntax involved.
1. Unary plus converts to number
console.log(+[]); // 0
console.log(+"5"); // 5
console.log(+true); // 1
+value tries to convert the value into a number.
[]becomes""when converted to a primitive string-like valueNumber("")is0- so
+[]is0
2. Array indexing
const outer = [[]];
console.log(outer[0]); // []
The array [[]] contains one element, which is [].
Step by Step Execution
Let us trace a simpler rewritten version of the expression.
const left = ++[[]][+[]];
const right = [+[]];
const result = left + right;
console.log(result);
Step 1: Evaluate +[]
+[]
[]is an empty array- unary
+converts it to a number - empty array becomes
"" Number("")is0
So:
+[] // 0
Step 2: Evaluate [[]][+[]]
Now substitute the 0:
[[]][0]
[[]]is an array with one element
Real World Use Cases
You probably should not write code like this in production, but the concepts behind it are very important.
1. Handling form input
Form fields often give you strings, even when the user typed a number.
const age = "42";
console.log(+age + 1); // 43
Understanding numeric conversion prevents accidental string concatenation.
2. Parsing API values
APIs may return values in mixed formats.
const price = "19.99";
const quantity = "2";
console.log(+price * +quantity); // 39.98
3. Debugging coercion bugs
A common bug is expecting math but getting strings.
console.log(1 + "2"); // "12"
console.log(1 + 2); // 3
Knowing how + behaves helps you find these issues quickly.
Real Codebase Usage
In real projects, developers usually avoid relying on surprising coercion. But they do use the underlying ideas in safer ways.
Explicit conversion
Instead of writing +value everywhere, many teams prefer clearer code:
const count = Number(inputValue);
This makes intent obvious.
Guard clauses for invalid numbers
const amount = Number(userInput);
if (Number.isNaN(amount)) {
throw new Error("Amount must be a valid number");
}
Validation before arithmetic
function addToCartQuantity(currentQty, incomingQty) {
const current = Number(currentQty);
const incoming = Number(incomingQty);
if (Number.isNaN(current) || Number.isNaN(incoming)) {
return currentQty;
}
return current + incoming;
}
Common Mistakes
1. Confusing numeric addition with string concatenation
Broken example:
console.log(1 + "2"); // "12"
Why it happens:
- one operand is a string
+switches to concatenation
Safer version:
console.log(1 + Number("2")); // 3
2. Assuming arrays behave like numbers
Broken example:
console.log([1] + 1); // "11"
Why it happens:
[1]becomes"1"- then string concatenation happens
3. Forgetting that [] can become 0
Comparisons
Related coercion behaviors
| Expression | Result | Why |
|---|---|---|
+[] | 0 | Empty array becomes empty string, then number 0 |
+"" | 0 | Empty string converts to 0 |
+true | 1 | Boolean true converts to 1 |
[] + [] | "" | Both arrays become empty strings |
Cheat Sheet
Quick reference
Unary plus
+value
- Converts
valueto a number +[]->0+"5"->5+true->1
Prefix increment
++value
- Converts
valueto a number - Adds
1 - Returns the new value
Array indexing
array[index]
[[]][0]->[]
Array to string conversion
String([]) // ""
String([])
([,])
FAQ
Why does +[] equal 0 in JavaScript?
Because unary + converts its operand to a number. An empty array becomes an empty string "", and Number("") is 0.
Why does [0] become "0"?
Arrays convert to strings by joining their elements with commas. A one-element array [0] becomes the string "0".
Why is the final result a string instead of a number?
Because the right side [+[]] becomes [0], which converts to "0" during +. Then 1 + "0" performs string concatenation, producing "10".
Does ++[] really work in JavaScript?
In an expression like this, yes. The value is coerced to a number, so [] becomes , then incremented to .
Mini Project
Description
Build a small JavaScript coercion explorer that prints the result of unusual expressions and explains their types. This helps you practice how arrays, strings, numbers, and operators interact in JavaScript.
Goal
Create a script that evaluates several coercion examples and shows both the result and the resulting type.
Requirements
- Create a function that logs an expression label, its result, and
typeofthe result. - Test at least five expressions involving arrays, unary
+, or string concatenation. - Include the expression
++[[]][+[]] + [+[]]. - Show at least one example that returns a number and one that returns a string.
Keep learning
Related questions
Adding Table Rows with jQuery: append(), Limits, and Best Practices
Learn how to add table rows in jQuery using append(), what elements are allowed in tables, and safer ways to build rows dynamically.
Bower vs npm: What’s the Difference in JavaScript Package Management?
Learn the plain difference between Bower and npm, how each manages packages, and why npm replaced Bower in most JavaScript projects.
Can `(a == 1 && a == 2 && a == 3)` Ever Be True in JavaScript?
Learn how JavaScript type coercion, loose equality, and custom object conversion can make `a == 1 && a == 2 && a == 3` true.