Recently I took a very interesting JavaScript quiz from Perfection Kills. I want to share my answers here.
(function(){ return typeof arguments; })();
a) "object" b) "array" c) "arguments" d) "undefined"
Answer: a
The arguments object is a local variable available within all functions. Please check out the explanation on MDN.
var f = function g(){ return 23; }; typeof g();
a) "number" b) "undefined" c) "function" d) Error
Answer: d
The function statement function g(...) {...} is shorthand for var g = function g(...) {...};
The function g has been assigned to the var f. So both g and g() are not defined.
(function(x){ delete x; return x; })(1);
a) 1 b) null c) undefined d) Error
Answer: a
The delete operator removes a property from an object. You cannot delete arguments inside a function. Please check out the explanation on MDN.
var y = 1, x = y = typeof x; x;
a) 1 b) "number" c) undefined d) "undefined"
Answer: d
The statement equals the following:
var y = 1;
y = typeof x; // “undefined”
x = y; // “undefined”
(function f(f){ return typeof f(); })(function(){ return 1; });
a) "number" b) "undefined" c) "function" d) Error
Answer: a
First of all, we pass a function var f = function f() {...} to a self-executing function (function g(f){...})(f);
Secondly, within the function g, we execute f(), which returns 1;
Finally, return typeof 1. So it's a "number".
var foo = { bar: function() { return this.baz; }, baz: 1 }; (function(){ return typeof arguments[0](); })(foo.bar);
a) "undefined" b) "object" c) "number" d) "function"
Answer: a
Same as previous question, we pass in a function. So arguments0 equals foo.bar(). But when foo.bar() executes, this refers to window object or globe object. So it returns undefined. Then again typeof undefined = "undefined".
var foo = { bar: function(){ return this.baz; }, baz: 1 } typeof (f = foo.bar)();
a) "undefined" b) "object" c) "number" d) "function"
Answer: a
Same reason as previous question. foo.bar() returns undefined. typeof undefined = "undefined".
var f = (function f(){ return "1"; }, function g(){ return 2; })(); typeof f;
a) "string" b) "number" c) "function" d) "undefined"
Answer: b
The question could be simplified as follow:
var a = (1, 2); a equals 2;
var a = (1,2,3); a equals 3;
var a = (1,2,3, ..., n); a equals n;
var x = 1; if (function f(){}) { x += typeof f; } x;
a) 1 b) "1function" c) "1undefined" d) NaN
Answer: c
The argument in if statement is a function expression, which is true. But "f" doesn't make a function declaration. So "f" doesn't enter the scope, which means there is no f() defined in scope.
var x = [typeof x, typeof y][1]; typeof typeof x;
a) "number" b) "string" c) "undefined" d) "object"
Answer: b
typeof typeof x. This always return "string". Please check out the explanation on MND.
(function(foo){ return typeof foo.bar; })({ foo: { bar: 1 } });
a) "undefined" b) "object" c) "number" d) Error
Answer: a
This is a tricky one. "foo" is confusing here. We can make it a bit clear:
(function(obj){
return typeof obj.foo.bar;
})({ foo: { bar: 1 } });
(function f(){ function f(){ return 1; } return f(); function f(){ return 2; } })();
a) 1 b) 2 c) Error (e.g. "Too much recursion") d) undefined
Answer: b
The second f() overrides the body. When execute f(), it returns 2.
function f(){ return f; } new f() instanceof f;
a) true b) false
Answer: b
'new' returns an instance of the class, unless the function that defines the class returns something else that is an object. So in this case, new f() just returns the function f itself and not an instance of it.
with (function(x, undefined){}) length;
a) 1 b) 2 c) undefined d) Error
Answer: b
Length is a property of a function object, and indicates how many arguments the function expects. Please check out the explanation on MDN.