Previous posts on Perfection Kills JavaScript quiz: 1, 2, 3, 4, 5, 6, 7, 8, 9.

10)

var x = [typeof x, typeof y][1];
typeof typeof x;

Correct answer: "string".

Compared to other questions in the quiz, this is a relatively easy one. Let's just go step by step parsing and executing the code.

Since the '=' operator assigns the value of its right operand to its left operand, we first need to evaluate the expression [typeof x, typeof y][1]. We are executing the first line of code, and neither x nor y variables have been defined so far. Hence, x has the value undefined and typeof x evaluates to the string "undefined". For the same reason typeof y also evaluates to "undefined".

The original snippet of code can be rewritten in the following way:

var x = ["undefined", "undefined"][1];
typeof typeof x;

From here, we get:

var x = "undefined"; //note that "undefined" is a string
typeof typeof x;

The variable x points to the string "undefined". Then typeof x is the string "string", and typeof typeof x is the string "string" too.

All posts on Perfection Kills JavaScript quiz: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14.