JavaScript
    About Lesson

    The var keyword in JavaScript is used to declare variables. It was the primary way to declare variables before the introduction of let and const in ES6. While var is still supported, it has limitations compared to modern alternatives.


    Syntax of var

    var variableName = value;

     

    • var: The keyword to declare a variable.
    • variableName: The name of the variable.
    • value: The initial value assigned to the variable (optional).

    Features of var

    Function Scope
    Variables declared with var are accessible throughout the function they are declared in.
    Example:

    function testVar() {
    var x = 10;
    console.log(x); // Outputs 10
    }
    testVar();
    console.log(x); // Error: x is not defined

     

    Hoisting
    Variables declared with var are “hoisted” to the top of their scope but are initialized with undefined.
    Example:

    console.log(a); // Outputs undefined
    var a = 5;

     

    Global Scope When Declared Outside Functions
    If var is declared outside a function, it becomes a property of the global object (e.g., window in browsers).
    Example:

    var globalVar = "I am global variable";
    console.log(window.globalVar); // Outputs: I am global variable

     

    Redeclaration Allowed
    A variable declared with var can be redeclared in the same scope without errors.
    Example:

    var name = "Junaid";
    var name = "Arsheen"; // No error
    console.log(name); // Outputs: Arsheen

     

    No Block Scope
    Variables declared with var do not respect block-level scoping.
    Example:

    if (true) {
    var color = "blue";
    }
    console.log(color); // Outputs: blue

     


     

    Example Usage of var

    1. Declaring a Variable

    var message = "Hello, World!";
    console.log(message); // Outputs: Hello, World!

     

    2. Function Scope

    function welcome() {
    var greeting = "Hi!";
    console.log(greeting); // Outputs: Hi!
    }
    welcome();
    console.log(greeting); // Error: greeting is not defined

     

    3. Hoisting

    console.log(num); // Outputs: undefined
    var num = 10;

    4. Redeclaration

    var age = 25;
    var age = 30; // Redeclaration allowed
    console.log(age); // Outputs: 30

     

    5. No Block Scope

    for (var i = 0; i < 3; i++) {
    console.log("Inside loop:", i); // Outputs: 0, 1, 2
    }
    console.log("Outside loop:", i); // Outputs: 3

     


    When to Use var

    1. Legacy Code: Use var if you need to maintain or update older JavaScript codebases.
    2. Global Variables (with caution): Use var only when necessary for global declarations.
    3. Avoid in New Code: For modern JavaScript projects, prefer let or const.

     

    Conclusion

    The var keyword is a fundamental part of JavaScript but has been largely superseded by let and const. Understanding var is crucial for working with older code and understanding JavaScript’s history, but for new projects, modern alternatives are recommended.