Plenty of π
Module 2: Variables, Data Types, Type Casting, and Comments
Declaring Variables: let, const, var

Variables are containers for storing data values. In JavaScript, you can declare variables using var, let, and const.

  • var (Older way, generally avoid in modern JS):

    • Function-scoped or globally-scoped.
    • Can be re-declared and updated.
    • Variables declared with var are hoisted (their declaration is moved to the top of their scope).
    var name = "Alice";
    var name = "Bob"; // Re-declaration allowed
    name = "Charlie"; // Update allowed
    
  • let (Modern way, preferred for variables that might change):

    • Block-scoped (scope is limited to the block {...} where they are defined).
    • Cannot be re-declared in the same scope.
    • Can be updated.
    • Not hoisted before initialization (Temporal Dead Zone).
    let age = 30;
    // let age = 31; // Error: Cannot re-declare block-scoped variable 'age'.
    age = 31; // Update allowed
    
  • const (Modern way, preferred for variables that should not change):

    • Block-scoped.
    • Cannot be re-declared or re-assigned after initialization.
    • Must be initialized during declaration.
    • Good for values that are constant, like mathematical constants or configuration settings.
    const PI = 3.14159;
    // PI = 3.14; // Error: Assignment to constant variable.
    // const PI = 3.14; // Error
    

Recommendation: Use const by default. If you know a variable needs to be reassigned, use let. Avoid using var in modern JavaScript development due to its sometimes confusing scoping and hoisting behavior.