0
0
Javascriptprogramming~10 mins

Variable hoisting behavior in Javascript - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a variable using var.

Javascript
console.log(x);
var [1] = 5;
Drag options to blanks, or click blank then click option'
Ax
By
Cz
Dw
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'let' or 'const' instead of 'var' here causes a ReferenceError when logging before declaration.
Declaring a different variable name than used in console.log.
2fill in blank
medium

Complete the code to show hoisting effect with var.

Javascript
console.log([1]);
var a = 10;
Drag options to blanks, or click blank then click option'
Ad
Ba
Cc
Db
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Logging a variable not declared with var causes ReferenceError.
Using a variable name different from the declared one.
3fill in blank
hard

Complete the code to log the variable declared with let (this will cause an error).

Javascript
console.log([1]);
let b = 20;
Drag options to blanks, or click blank then click option'
Ab
Ba
Cc
Dd
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using a variable name different from the declared one.
Expecting var-like hoisting behavior with let.
4fill in blank
hard

Fill both blanks to create a function that logs a hoisted variable.

Javascript
function test() {
  console.log([1]);
  var [2] = 30;
}
test();
Drag options to blanks, or click blank then click option'
Ax
By
Cz
Dw
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using different variable names in console.log and declaration.
Using let or const instead of var for hoisting.
5fill in blank
hard

Fill all three blanks to create a block with let and var variables and log the hoisted one.

Javascript
{
  var [1] = 40;
  let [2] = 50;
}
console.log([3]);
Drag options to blanks, or click blank then click option'
Aa
Bb
Cc
Dd
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Logging the let variable outside its block scope.
Using different variable names inconsistently.