JavaScript wart: nested brackets in property notation
Nested brackets in property notation
js puzzle of the day. what does this print.
// what does this print. const x = ["a", "b", "c"]; console.assert(x[[0]] === "a"); console.assert(x[[[0]]] === "a"); console.assert(x[[[[0]]]] === "a"); /* the x[[0]] eval to x["0"]. because [0] is a array, of 1 item that is number 0. when forced to string, it becomes "0". JavaScript force it to string because array index is property key of string type of integer. [0] becomes "0" because array stringify into numbers separated by comma e.g. [1,2] becomes "1,2". so [0] becomes "0". */ console.assert(String([0]) === "0"); console.assert(String([1, 2]) === "1,2");