DEV Community

Cover image for JavaScript Quiz #02 — let, const, and var
Quizzesforyou
Quizzesforyou

Posted on • Updated on • Originally published at Medium

JavaScript Quiz #02 — let, const, and var

Checkout the interactive quiz of this article here https://quizzesforyou.com/quiz/constletvar

In JavaScript, variables can be declared using three different methods: var, let, and const.

var :

  • Variables declared with var are globally scoped or function scoped.

  • They can be re-declared and updated within their scope.

  • Hoisting behavior: var variables are hoisted to the top of their scope and initialized with undefined.

     console.log(greeting); // Output: undefined 
    var greeting = "Good day";
    

let :

  • Variables declared with let are block-scoped, limiting their accessibility to the block where they are defined.

  • They can be updated within their scope but cannot be re-declared in the same scope

  • Hoisting behavior: let variables are hoisted to the top of their scope but are not initialized.

 console.log(greeting); // Error: Reference Error: greeting is not defined
 let greeting = "Good day";
Enter fullscreen mode Exit fullscreen mode

Const:

  • Variables declared with const are also block scoped.

  • They cannot be updated or re-declared once defined.

  • const variables must be initialized at the time of declaration.

  • For objects declared with const, the properties of the object can be updated, but the variable itself cannot be reassigned.

  • const variables are not hoisted to the top of their scope. If you try to access a const variable before its declaration, you will encounter a reference error.

 console.log(greeting); // Error: Reference Error: greeting is not defined                 
 const greeting = "Good day";
Enter fullscreen mode Exit fullscreen mode

Hoisting:

  • Hoisting is a JavaScript mechanism where variable and function declarations are moved to the top of their containing scope during the compilation phase.

  • Only the declarations are hoisted, not the initializations or assignments.

  • Variable declarations using var are hoisted to the top of their scope and are initialized with the value undefined by default.

  • Variable declarations using let and const are hoisted to the top of their scope, but they are not initialized. Accessing them before their declaration results in a ReferenceError.

Scope:

  • var variables are function scoped, meaning they are accessible within the function where they are defined.

  • let and const variables are block-scoped, meaning they are accessible only within the block where they are defined.


Checkout the interactive quiz of this story here https://quizzesforyou.com/quiz/constletvar

1. What is the scope of a variable declared with var?

a) Block scope

b) Global scope

c) Function scope

Answer: c) Function scope

2. Which variable/constant declaration is hoisted to the top of its scope during the compilation phase?

a) var

b) let

c) const

d) All of them

Answer: a) var

Explanation: Variables declared with var are hoisted to the top of their scope during the compilation phase.

3. What is the initial value of a let (before declaration)?

a) null

b) undefined

c) 0

Answer: b) undefined

4: What is the output of the following code snippet?

const x = 10;

if (true) { const x = 20; console.log(x); }

console.log(x);
Enter fullscreen mode Exit fullscreen mode

a) 10 20

b) 20 10

c) undefined

d) ReferenceError: x is not defined

Answer: b) 20 10

The code declares a constant x with a value of 10. Inside the if statement, a new block scope is created with another constant x assigned a value of 20. The first console.log(x) statement inside the if block will output 20, as it refers to the x variable in that block. The second console.log(x) statement outside the if block will output 10, as it refers to the x variable in the outer scope.

5. Which of the below cannot be re-declared in the same scope?

a) let

b) const

c) Both let and const

Answer: c) Both let and const

Both let and const declarations cannot be re-declared in the same scope. Once a variable is declared with let or const, attempting to re-declare it with the same name will result in an error. This helps prevent accidental redeclaration and promotes better code clarity and predictability.

6.What happens if you try to access a const before its declaration?

a) It throws a ReferenceError.

b) It returns undefined.

c) It gives the value assigned to it.

d) It depends on the context.

Answer: a) It throws a ReferenceError.

Explanation: Accessing a const variable before its declaration results in a ReferenceError. Unlike variables declared with var that are hoisted.

7.Which variable declaration is recommended for most use cases in modern JavaScript?

a) var

b) let

c) const

d) All of them are equally recommended.

Answer: b) let

In most use cases, the let variable declaration is recommended for modern JavaScript. This is because let provides block scope, allowing for better control over variable visibility and reducing the risk of unintended side effects.

8. What is the difference between var and let in terms of hoisting?

a) Both var and let variables are hoisted to the top of their scope.

b) var variables are hoisted with the value undefined, while let variables are not initialized during hoisting.

c) var variables are not hoisted, while let variables are hoisted to the top of their scope.

d) There is no difference in hoisting behavior between var and let.

Answer: b) var variables are hoisted with the value undefined, while let variables are not initialized during hoisting.


Check more quizzes@ https://quizzesforyou.com/

References: https://developer.mozilla.org/en-US/docs/Learn/Getting_started_with_the_web/JavaScript_basics

Article posted using bloggu.io. Try it for free.

Top comments (0)