JS quiz Flashcards
Which fn will be called from line 10? 1| if (true) { 2| function fn() { 3| return 10; 4| } 5| } else { 6| function fn() { 7| return 20; 8| } 9| } 10| fn();
answer is 20, because function is defined
twice during parsing, so one on line 6 one is used.
Which fn will be called from line 11? 1| var fn; 2| if (false) { 3| fn = function() { 4| return 30; 5| }; 6| } else { 7| fn = function() { 8| return 40; 9| }; 10| } 11| fn();
answer is 40, because fn is assigned on
line 7, during execution.
What is the output of this program? 1| if (false) { 2| var fn = function() { 3| return 70; 4| }; 5| } else { 6| var fn = function() { 7| return 80; 8| }; 9| } 10| fn();
answer is 80, because fn is defined on line 6,
during execution of the true part.
What will fpn return? 1| function fpn(){ 2| return 3| { 2 + 3 }; 4| } 5| fpn(2+3);
answer is undefined, because fpn returns undefined on line 2, a semicolon isn’t needed to end the statement.
What is the output of this program, and what does it do in general?
(function (n,a,b) { return n>0 ?
arguments.callee(n-1,b,a+b) : a; })(10,0,1);
answer is 55
What is the output of this program? 1| var fn22 = 1; 2| function gn22() { 3| if (!fn22) { 4| var fn22 = 2; 5| } 6| console.log(fn22); 7| } 8| gn22();
answer is 2, because fn22 on line 1 is a global,
while fn22 on line 3 is different and local to gn22.
What is the output of this program? 1| var a23 = 1; 2| function b24() { 3| a23 = 10; 4| return; 5| function a23() { 6| return 11; 7| } 8| } 9| b24(); 10| console.log(a23);
answer is 1, because inner function a23 on line 5
was hoisted and changed to 10 on line 3;
global a23=1 on line 1 is unaffected
What is the output of this program? 1| var a44 = 1; 2| console.log(a44); 3| if (true) { 4| var a44 = 2; 5| console.log(a44); 6| } 7| console.log(a44);
answer is 1 2 1, because only one global var a44,
inner block doesn’t define a local variable.
What is the output of this program? 1| function f28() { 2| var x56 = 94; 3| if (false) { 4| (function () { 5| var x56 = 83; 6| console.log(x56); 7| }()); 8| console.log(x56); 9| } 10| console.log(x56); 11| } 12| f28();
answer is 94, because anonymous function creates a new local var x56=83, global x56 remains 94.
What is the output of this program? 1| function main() { 2| try { f77(); } catch(e) { console.log(e); }; 3| try { b78(); } catch(e) { console.log(e); }; 4| var f77 = function () { 5| console.log("f77"); 6| } 7| function b78() { 8| console.log("b78"); 9| } 10| } 11| main();
answer is Error, b78, because both declarations on line 4 and line 7 are hoisted to top,
but var f77 is only assigned on line 4, during execution.
What is the output of this program? 1| f90(); 2| function f90() { 3| console.log('f90'); 4| };
answer is f90, because function f90 hoisted to top
and available on line 1.
What is the output of this program? 1| f91(); 2| var f91 = function () { 3| console.log('f91'); 4| };
answer is Error, because var f91 is hoisted, but
var f91 is used before being assigned.
What is the output of this program? 1| var x = 0; 2| function hn() { 3| x++; 4| console.log(x); 5| return function hn() { 6| x+=10; 7| console.log(x); 8| } 9| } 10| hn(hn())(hn())
answer is 1 2 3 13, because only one global variable x,
there are two hn functions in different scope.
What is the output of this program? Do fn gn and kn update the same num? (What is a closure?) 1| function fn() { 2| var num = 1; 3| var gn = function() { 4| console.log(num); 5| num+=10; 6| } 7| var kn = function() { 8| console.log(num); 9| num+=100; 10| } 11| num += 3000; 12| return [gn,kn]; 13| } 14| gn = fn()[0]; 15| kn = fn()[1]; 16| gn(gn())*kn(kn())
answer is 3001 3011 3001 3101, because local var num is in a closure,
separate copies for gn and kn.
If you remove the var on line 2,
you get: 3001 3011 3021 3121.
If you move line 2 to line 1,
you get 6001 6011 6021 6121.