Get the latest tech news

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


This is valid and returns the string "10" in JavaScript (more examples here): console.log(++[[]][+[]]+[+[]]) Why? What is happening here?

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. 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. 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.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of string

string

Related news:

News photo

A popular but wrong way to convert a string to uppercase or lowercase

News photo

Apple Rethinks Its Movie Strategy After a String of Misses

News photo

Sellafield, World's Largest Store of Plutonium, Apologizes After Guilty Plea Over String of Cybersecurity Failings