Bitsy Explains
Home/Cursed JavaScript/Lesson 1

Why [] + [] Is an Empty String

Why [] + [] gives an empty string, why "3" + 1 gives "31", and how to make the + sign do real math instead of gluing text together.

Lesson 1 of 103 min readVideo
Video thumbnail: Why [] + [] Is an Empty String Watch the 90-second video
Explain like I'm five

The plus sign has two jobs. It can add numbers, or it can glue pieces of text together. When it gets something that is not a number, like an array or an object, it turns it into text first and then glues. So two empty arrays become two empty pieces of text, and gluing nothing to nothing gives you nothing.

The curse

Try these two lines in Node or in your browser console.

console.log(JSON.stringify([] + [])); // prints: ""
console.log([] + {}); // prints: [object Object]

The first line gives an empty string. JSON.stringify is only there so you can see the quotes, because an empty string would print as a blank line. The second line gives the text [object Object]. There are no numbers anywhere, and there is no error either.

What + does, step by step

When JavaScript runs a + b, it follows the same steps every time.

  • First, each side is turned into a primitive value, which means a plain number, string or boolean. Numbers and strings stay as they are. Arrays and objects get converted.
  • An array becomes text by joining its items with commas. An empty array has no items, so it becomes "".
  • A plain object becomes the text "[object Object]".
  • Then, if either side is now a string, both sides are turned into strings and glued together.
  • Only if neither side is a string does + do real addition.

So [] + [] becomes "" + "", which is "". And [] + {} becomes "" + "[object Object]".

console.log([1, 2] + [3]); // prints: 1,23
console.log([] + 5); // prints: 5
console.log(true + 1); // prints: 2

[1, 2] becomes "1,2" and [3] becomes "3", so gluing gives "1,23". The second line looks like the number 5, but it is the string "5". In the last line neither side is text, so true becomes 1 and you get real math.

Same curse, with numbers

The same rule bites when numbers arrive as text, which happens all the time. Values from form fields, URLs and many files are strings, even when they look like numbers.

console.log("3" + 1); // prints: 31
console.log("3" - 1); // prints: 2
console.log("3" * "2"); // prints: 6

With +, one side is a string, so JavaScript glues and you get "31". The minus sign only knows math, so it turns both sides into numbers and gives 2. The same goes for *, / and %. That is why mixing strings and numbers feels so random. Plus is the only math operator that prefers text.

How to break the curse

Turn text into a number before you add.

const apples = "3";
console.log(Number(apples) + 1); // prints: 4
console.log(Number("hello")); // prints: NaN

Number() converts a whole string into a number. If the text is not a number, you get NaN, JavaScript's broken-number value, so check what came back when the input might be messy.

A good habit is to convert once, right where the value enters your program, for example as soon as you read a form field. After that you only work with real numbers, and the plus sign always adds.

Rule of thumb

The + sign glues text, so if you want math, make sure both sides are numbers.

Common mistake

Reading a number from a form field and adding to it straight away, like input.value + 1. Form values are always strings, so if the field holds "5" you get "51" instead of 6. Convert with Number(input.value) first.

Check yourself

What does "3" + 1 give in JavaScript?