Previous posts on Perfection Kills JavaScript quiz: 1, 2, 3.
4)
var y = 1, x = y = typeof x;
x;
Correct answer: "undefined".
The '=' operator in JavaScript has right associativity, which means that x = y = typeof x
is equivalent to x = (y = typeof x)
. First, the expression typeof x
is evaluated. Since x has not been defined yet, typeof x
evaluates to the string "undefined". After that, y is assigned the value of "undefined", and then x is assigned the same value. As a result, when the second line of code in this snippet is run, x has the value of "undefined" (a string).
All posts on Perfection Kills JavaScript quiz: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14.