Recently I took the 'Perfection Kills' JavaScript quiz (http://perfectionkills.com/javascript-quiz/) and found it quite insightful. I thought it would be interesting to go through the solution to some of the quiz problems.
Attention: spoilers ahead.
All the questions are structured in the same way. There is a snippet of code and one has to predict what it evaluates to.
1)
(function(){
return typeof arguments;
})();
Correct answer: "object".
First of all, we see parentheses immediately after the function definition. This means that our function is immediately invoked, and our snippet of code returns the return value of the anonymous function, i.e. typeof arguments
. 'arguments' is an array-like object of all arguments passed to a function. In our case, as no arguments are passed to the function (there is nothing in the last pair of parentheses), 'arguments' object looks like an empty array. The typeof operator with an array or an object returns the string "object".
All posts on Perfection Kills JavaScript quiz: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14.