Announcement

Collapse
No announcement yet.

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

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

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

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

  • #2
    Variables declared by the "var" keyword are scoped to the immediate function body (function scope) while "let" variables are scoped to the immediate enclosing block denoted by {....} (block scope).

    Variable declared with var keyword can be re-declared and updated in the same scope while variable declared with let keyword can be updated but not re-declared.

    Variables defined with let are hoisted to the top of the block, but not initialized. This means that the block of code is aware of the variable, but it cannot be used until it has been declared. So, using a let variable before it is declared will result in a ReferenceError.

    Comment


    • #3

      let and const hoisting

      Variables declared with let and const are also hoisted but, unlike var , are not initialized with a default value. An exception will be thrown if a variable declared with let or const is read before it is initialized

      Comment

      Related Topics

      Collapse

      Working...
      X