JavaScript Variable Declaration: var, let, const

Jul 28, 2024

JavaScript Variable Declaration: var, let, and const

Overview

  • Three ways to declare variables in JavaScript: var, let, const.
  • var has been in JavaScript from the beginning.
  • let and const were introduced in ES6 to improve upon the limitations of var.

Importance

  • Understanding how they work is crucial, especially for technical interviews.
  • Interviewers often evaluate JavaScript knowledge based on these concepts.

Types of Questions

  1. Scope

    • Scope refers to the visibility of variables in different parts of the code.

    • Types of scopes:

      • Global Scope
      • Function Scope
      • Block Scope (unique to let and const)
    • Example of var:

      var a = 5;  
      console.log(a); // 5  
      
    • Example of let:

      {  
          let b = 10;  
      }  
      console.log(b); // ReferenceError  
      
    • Example of const:

      {  
          const c = 15;  
      }  
      console.log(c); // ReferenceError  
      
  2. Variable Shadowing

    • Refers to when a variable in a certain scope has the same name as another variable in an outer scope.

    • Example:

      var hello = 'Hello';  
      {  
          let hello = 'Hi';  
          console.log(hello); // High  
      }  
      console.log(hello); // Hello  
      
    • Note: Cannot shadow let or const with var (illegal shadowing).

  3. Declaration

    • var: Can be redeclared without error.
    • let: Cannot be redeclared in the same scope; results in an error.
    • const: Same behavior as let, with an additional error for initializing without a value.
    • Example:
      var a = 1;  
      var a = 2; // No error  
      let b = 1;  
      let b = 2; // Error  
      const c = 1;  
      const c = 2; // Error  
      
  4. Initialization

    • var: Can be declared without initial value.
    • let: Can be declared without initial value.
    • const: Must be initialized at declaration; otherwise, it results in an error.
  5. Hoisting

    • Process where declarations are moved to the top of their scope during creation phase.
    • var: Hoisted, initialized to undefined.
    • let and const: Hoisted but cannot be accessed before initialization (causing error - Temporal Dead Zone).
    • Example of hoisting in var:
      console.log(a); // undefined  
      var a = 5;  
      
    • Example of hoisting in let/const:
      console.log(b); // ReferenceError  
      let b = 10;  
      

Temporal Dead Zone

  • The period between the declaration and initialization of let and const variables where they exist but cannot be accessed.

Conclusion

  • Questions about var, let, and const can cover various aspects, including scope, declaration, initialization, and hoisting.
  • Understanding these key concepts is valuable for tackling JavaScript interviews effectively.

Additional Notes

  • The discussion included recommendations to follow for more insights and potential future topics to cover.