Difference between let and var in JavaScript

Difference between let and var in JavaScript
----------------------------------------------

ES2015 introduced let keywords

Both var and let are used for variable declaration in javascript but the major difference between them is that var is function scoped and let is block scoped.

Variable declared with var is defined throughout the program as compared to let.

First lets take a look in var keyword.

Key points about var keyword

  • It has global scrope,If you declare glbally and it is accessiable throughout the code.
  • It can redeclared with the same variable name.
  • It get hoisted

 

Now let keyword

Lets see how to define let keyword

2. let keyword does not have global scope and also it is not hoisted as var.

Example.

3.It cannot be declared like 

So the Key points about let keyword

  • It cab be in global scrope,but cannot be accessed globally.
  • It was added in ES6 (ES 2015) version JS
  • It cannot be redeclared with the same variable name.
  • It will not get hoisted

 

Categories: Java Script Tags: #ES6, #JavaScript,

Newsletter Subcribe

Receive updates and latest news direct from our team. Simply enter your email.