Why does ++[[]][+[]]+[+[]] return the string "10"?

This is valid and returns the string "10" in JavaScript (more examples here):


No. 424: Why Does? Why Is?, Why Do Clocks run Clockwise? asks the title of a book by David Feldman. Why indeed! This odd book looks at 239 such questions. First they look innocent  The most scientifically backed theory about why we yawn is brain temperature regulation. A 2014 study published in the journal Physiology & Behavior looked at the yawning habits of 120 people and

++[[]][+[]] => 1 // [+[]] = [0], ++0 = 1
[+[]] => [0]

Then we have a string concatenation

1+[0].toString() = 10

A Question That Won't Go Away: Why Does Trump Love Putin So , In June 2013, after announcing that his Miss Universe pageant would be held that year in Moscow, Trump tweeted, “Do you think Putin will be  Does it take you forever to make a doctor’s appointment, clean out your garage, or do your taxes? Putting off something that needs to be done is called procrastination.We all do it sometimes.

The following is adapted from a blog post answering this question that I posted while this question was still closed. Links are to (an HTML copy of) the ECMAScript 3 spec, still the baseline for JavaScript in today's commonly used web browsers.

First, a comment: this kind of expression is never going to show up in any (sane) production environment and is only of any use as an exercise in just how well the reader knows the dirty edges of JavaScript. The general principle that JavaScript operators implicitly convert between types is useful, as are some of the common conversions, but much of the detail in this case is not.

The expression ++[[]][+[]]+[+[]] may initially look rather imposing and obscure, but is actually relatively easy break down into separate expressions. Below I’ve simply added parentheses for clarity; I can assure you they change nothing, but if you want to verify that then feel free to read up about the grouping operator. So, the expression can be more clearly written as

( ++[[]][+[]] ) + ( [+[]] )

Breaking this down, we can simplify by observing that +[] evaluates to 0. To satisfy yourself why this is true, check out the unary + operator and follow the slightly tortuous trail which ends up with ToPrimitive converting the empty array into an empty string, which is then finally converted to 0 by ToNumber. We can now substitute 0 for each instance of +[]:

( ++[[]][0] ) + [0]

Simpler already. As for ++[[]][0], that’s a combination of the prefix increment operator (++), an array literal defining an array with single element that is itself an empty array ([[]]) and a property accessor ([0]) called on the array defined by the array literal.

So, we can simplify [[]][0] to just [] and we have ++[], right? In fact, this is not the case because evaluating ++[] throws an error, which may initially seem confusing. However, a little thought about the nature of ++ makes this clear: it’s used to increment a variable (e.g. ++i) or an object property (e.g. ++obj.count). Not only does it evaluate to a value, it also stores that value somewhere. In the case of ++[], it has nowhere to put the new value (whatever it may be) because there is no reference to an object property or variable to update. In spec terms, this is covered by the internal PutValue operation, which is called by the prefix increment operator.

So then, what does ++[[]][0] do? Well, by similar logic as +[], the inner array is converted to 0 and this value is incremented by 1 to give us a final value of 1. The value of property 0 in the outer array is updated to 1 and the whole expression evaluates to 1.

This leaves us with

1 + [0]

... which is a simple use of the addition operator. Both operands are first converted to primitives and if either primitive value is a string, string concatenation is performed, otherwise numeric addition is performed. [0] converts to "0", so string concatenation is used, producing "10".

As a final aside, something that may not be immediately apparent is that overriding either one of the toString() or valueOf() methods of Array.prototype will change the result of the expression, because both are checked and used if present when converting an object into a primitive value. For example, the following

Array.prototype.toString = function() {
  return "foo";

... produces "NaNfoo". Why this happens is left as an exercise for the reader...

COVID-19: Considerations for Wearing Cloth Face Coverings, COVID-19 can be spread by people who do not have symptoms and do not know that they are infected. That's why it's important for everyone to  Why do you press harder on a remote-control when you know the battery is dead? Why is it that in the US: If you take off all your clothes and walk down the street waving a machete and firing an Uzi, terrified citizens will phone the police and report: "There's a naked person outside!"

Let’s make it simple:

++[[]][+[]]+[+[]] = "10"

var a = [[]][+[]];
var b = [+[]];

// so a == [] and b == [0]


// then a == 1 and b is still that array [0]
// when you sum the var a and an array, it will sum b as a string just like that:

1 + "0" = "10"

Coronavirus (COVID-19) frequently asked questions, Can you contract the coronavirus disease from a package in the mail? The coronavirus pandemic has hit a giant pause button on fans being able to watch golf on TV, and in some cases, even kept people off courses. But while we hunker down and hope for a speedy return

Coronavirus disease 2019 (COVID-19), Coronaviruses are thought to be spread most often by respiratory droplets. Although the virus can survive for a short period on some surfaces, it is unlikely to be spread from domestic or international mail, products or packaging. Why do I fart so much? Sometimes you may experience more flatulence than usual. Increased farting can stem from a natural body reaction, or in some cases, an underlying medical condition.

Coronavirus disease (COVID-19) advice for the public: Myth busters, Although for most people COVID-19 causes only mild illness, it can make some people very ill. More rarely, the disease can be fatal. Older people, and those with pre- existing medical conditions (such as high blood pressure, heart problems or diabetes) appear to be more vulnerable. Coffee itself does not make people tired, but the caffeine in coffee and its effects on the body can sometimes cause tiredness.. Some reasons that drinking a cup of coffee might make someone feel

ISSUE#2, The new coronavirus is a respiratory virus which spreads primarily through droplets generated when an infected person coughs or sneezes, or through droplets of saliva or discharge from the nose. To protect yourself, clean your hands frequently with an alcohol-based hand rub or wash them with soap and water. But why does the air move at all? You might be wondering why the air would move from high pressure to low pressure in the first place. This is something that happens in nature all the time: things always try to even out. It’s called diffusion. Even people do it! When people get onto a bus, do they all sit on the same side of the bus first?

Why does sex hurt?, There is no evidence that drinking lots of water flushes out the new coronavirus or the stomach acid kills the virus. However, for good health in general, it is recommended that people should have adequate water every day for good health and to prevent dehydration. Here’s why to do, and how to do it properly. Share this article via email with one or more people using the form below. To * From * Yes! Send me expert insights each week in Health Essentials News.