LearnFrenzy
Sign in Take a free test

Scope and Hoisting · Exercise 2 of 4

Scope and Hoisting Predict the Output

  • 20 questions
  • Page 1 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Read each JavaScript program and choose what it prints. Every program was run on Node.js as a normal (non-strict) script unless it says "use strict", so the answers are the real output.

Question 1 Medium

What does the following JavaScript code print (console output, as in Node.js)?

main.js
const level = "global";
function show() {
  return level;
}
function run() {
  const level = "local";
  return show();
}
console.log(run());
A local
B undefined
C global
D ReferenceError
Question page & discussion
Question 2 Medium

What does the following JavaScript code print (console output, as in Node.js)?

main.js
let x = 1;
function a() {
  let x = 2;
  return function b() {
    x++;
    return x;
  };
}
const f = a();
console.log(f(), f(), x);
A 3 4 1
B 2 3 1
C 3 4 3
D 2 2 1
Question page & discussion
Question 3 Hard

What does the following JavaScript code print (console output, as in Node.js)?

main.js
function test() {
  console.log(v);
  if (false) {
    var v = 10;
  }
  console.log(v);
}
test();
A undefined undefined
B ReferenceError
C undefined 10
D 10 10
Question page & discussion
Question 4 Hard

What does the following JavaScript code print (console output, as in Node.js)?

main.js
function f() {
  return typeof g;
  var g = 5;
  function g() {}
}
console.log(f());
A number
B undefined
C function
D ReferenceError
Question page & discussion
Question 5 Medium

What does the following JavaScript code print (console output, as in Node.js)?

main.js
var greet = "hello";
function greet() {
  return "fn";
}
console.log(typeof greet);
A function
B undefined
C string
D object
Question page & discussion
Question 6 Medium

What does the following JavaScript code print (console output, as in Node.js)?

main.js
function counter() {
  let c = 0;
  return () => ++c;
}
const c1 = counter();
const c2 = counter();
c1();
c1();
console.log(c1(), c2());
A 3 1
B 3 4
C 1 1
D 2 1
Question page & discussion
Question 7 Easy

What does the following JavaScript code print (console output, as in Node.js)?

main.js
const name = "global";
{
  const name = "block";
  console.log(name);
}
console.log(name);
A block global
B block block
C global global
D SyntaxError
Question page & discussion
Question 8 Easy

What does the following JavaScript code print (console output, as in Node.js)?

main.js
function outer() {
  function inner() {
    var secret = 42;
  }
  inner();
  return typeof secret;
}
console.log(outer());
A number
B undefined
C 42
D ReferenceError
Question page & discussion
Question 9 Easy

What does the following JavaScript code print (console output, as in Node.js)?

main.js
var total = 10;
function update() {
  total = 20;
  var local = 5;
}
update();
console.log(total, typeof local);
A 10 undefined
B 20 number
C 10 number
D 20 undefined
Question page & discussion
Question 10 Hard

What does the following JavaScript code print (console output, as in Node.js)?

main.js
function test() {
  console.log(typeof x);
  var x = 10;
  function x() {}
  console.log(typeof x);
}
test();
A function number
B undefined number
C number function
D function function
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links