Languages
[Edit]
EN

JavaScript - difference between let and var

4 points
Created by:
Dollie-Rutledge
806

In this article, we would like to explain the difference between let and var keywords in JavaScript.

The main differences between let and var keywords

Edit

1. Variable hoisting support

Edit

The var keyword variables support variable hoisting.

The let keyword variables do not support variable hoisting.

Definition:

Variable hoisting is automatic shifting variable declarations to the top of the source code, making them accessible in the above of the declarations.

 

Example 1 (variable hoisting):

Output:

 

Example: 2 (variable hoisting):

Output:


Example 3 (normal usage):

Output:

 

2. The let keyword variables can be created in different scopes:

Edit
 let keyword variablesvar keyword variables
global scopeyesyes
function scopeyesyes
curly braces scopeyesno

 

Tips

Edit

Use let keyword instead of var to avoiding possible errors related with scopes.

See also

Edit
  1. What is scope in JavaScript?

Alternative titles

  1. JavaScript - let vs var
  2. JavaScript - what is the difference between let and var?
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join