What is the difference between using “let” and “var”?

What is the difference between using “let” and “var”?

Add Comment


  • 2 Answer(s)

    The difference is scoping. var is scoped to the near by function block and let is scoped to the near by enclosing block, which can be lesser than a function block. Both are global if outside any block.

    Answered on May 4, 2019.
    Add Comment

    var and let are both used for function declaration in javascript but the difference between them is that var is function scoped and let is block scoped.
    It can be said that a variable declared with var is defined throughout the program as compared to let.
    An example will clarify the difference even better
    Example of var:

    Input:
    console.log(x);
    var x=5;
    console.log(x);
    Output:
    undefined
    5
    Answered on June 10, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.