JavaScript Hoisting and Variable Declarations Quiz

Create an account and save your quiz results

Login and save your results

OR

Question 1/7

Consider this code: Which value will be logged to the console?
var x = 1;
function testHoist() {
    console.log(y);
    var y = 5;
}
testHoist();

Select your answer

Question 2/7

What is the output of the following JavaScript code snippet?
console.log(a);
var a = 10;
console.log(b);
let b = 20;

Select your answer

Question 3/7

Which statement about variable hoisting in JavaScript is true?

Select your answer

Question 4/7

Which of the following correctly illustrates variable hoisting with 'var'?

Select your answer

Question 5/7

What will be the output of the code below?
console.log(foo);
var foo = function() {
    return "Hello";
};

Select your answer

Question 6/7

What will be the output of the following code?
console.log(typeof myVar);
var myVar = 42;

Select your answer

Question 7/7

Which variable declaration will throw a ReferenceError if accessed before initialization?

Select your answer

Your Results

You did not answer any questions correctly.

Your Answers

Question 1/7
😊 Your answer was correct 🙁 Your answer was incorrect
Consider this code: Which value will be logged to the console?
var x = 1;
function testHoist() {
    console.log(y);
    var y = 5;
}
testHoist();

Available answers

Inside the
testHoist
function, the variable
y
is declared with
var
. Because
var
is hoisted, the declaration is processed at the top of the function scope, but the assignment
y = 5
is not, so it logs
undefined
.
Question 2/7
😊 Your answer was correct 🙁 Your answer was incorrect
What is the output of the following JavaScript code snippet?
console.log(a);
var a = 10;
console.log(b);
let b = 20;

Available answers

var a
is hoisted, so the initial
console.log(a)
evaluates to
undefined
. After the assignment
a = 10
, the second
console.log(a)
outputs
10
.
let b
is not hoisted, so
console.log(b)
before its declaration results in a
ReferenceError
.
Question 3/7
😊 Your answer was correct 🙁 Your answer was incorrect
Which statement about variable hoisting in JavaScript is true?

Available answers

Only the declarations of variables declared with
var
are hoisted to the top of their function or global scope. Functions declared using function declarations (not expressions) are hoisted with their definitions, but
let
and
const
variables are hoisted in a manner that prevents access before the line of code they are defined on.
Question 4/7
😊 Your answer was correct 🙁 Your answer was incorrect
Which of the following correctly illustrates variable hoisting with 'var'?

Available answers

In the
var
declaration, the variable
a
is hoisted to the top of its scope as an undefined variable. However, the proper illustration of hoisting explicitly declares
var a;
before it is used.
Question 5/7
😊 Your answer was correct 🙁 Your answer was incorrect
What will be the output of the code below?
console.log(foo);
var foo = function() {
    return "Hello";
};

Available answers

The variable
foo
is declared using
var
, so it is hoisted to the top of the scope with an initial value of
undefined
. Therefore,
console.log(foo)
outputs
undefined
because the function expression hasn't been evaluated yet.
Question 6/7
😊 Your answer was correct 🙁 Your answer was incorrect
What will be the output of the following code?
console.log(typeof myVar);
var myVar = 42;

Available answers

The
typeof
operator will return
undefined
because
myVar
is hoisted but not initialized at the time of the
console.log
.
Question 7/7
😊 Your answer was correct 🙁 Your answer was incorrect
Which variable declaration will throw a ReferenceError if accessed before initialization?

Available answers

Variables declared with
let
and
const
are not hoisted in the traditional sense and will throw a
ReferenceError
if accessed before they are initialized.