Understanding the JavaScript Interview Question: console.log(([][[]] + [])[+!![]]+([]+{})[+!![]+ + !![]])

This article explains how to evaluate the puzzling JavaScript expression console.log(([][[]] + [])[+!![]]+([]+{})[+!![]+ + !![]]), detailing the required knowledge of type coercion, unary operators, and implicit conversions to reveal why the output is “nb”.

php Courses
php Courses
php Courses
Understanding the JavaScript Interview Question: console.log(([][[]] + [])[+!![]]+([]+{})[+!![]+ + !![]])

The interview question asks what the output of console.log(([][[]] + [])[+!![]]+([]+{})[+!![]+ + !![]]) is. It tests understanding of type conversion, implicit coercion, and operator behavior in JavaScript.

Knowledge required to solve this problem

The unary + operator performs the same conversion as Number().

When + is used for addition and either operand is an object, number, or boolean, the engine calls toString() on the operands.

The logical NOT operator ! always returns a boolean.

Double NOT !! forces a value to a boolean.

Solving the expression ([][[]] + [])[+!![]]+([]+{})[+!![]+ + !![]]

First, evaluate ([][[]] + []). Inside this, [][[]] accesses an empty array with a key of []. The empty array is converted to an empty string, so the property name becomes "", which does not exist, yielding undefined. Adding [] to undefined triggers implicit conversion: undefined becomes the string "undefined" and [] becomes "", resulting in the string "undefined".

Next, evaluate [+!![]]. !![] converts an empty array to true; the unary + then converts true to the number 1. Thus [+!![]] is [1], and ([][[]] + [])[+!![]] accesses the character at index 1 of "undefined", which is "n".

Then evaluate ([]+{})[+!![]+ + !![]]. The sub‑expression []+{} concatenates an empty array and an empty object, producing the string "[object Object]". The index expression +!![]+ + !![] evaluates to 1+1, i.e., 2. Accessing index 2 of "[object Object]" yields the character "b".

Finally, concatenate the two characters obtained: "n" + "b" = "nb". Therefore the console logs nb.

In summary, the difficulty of this question often stems from not understanding the evaluation order and how expressions like [][[]] are interpreted: the empty array’s key [] is coerced to an empty string, leading to undefined, which then participates in further implicit conversions.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

debuggingFrontendJavaScriptinterviewType Coercion
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.