Codementor Events

Var VS Let in Javascript.

Published Oct 26, 2021
Var VS Let in Javascript.

In Javascript, you can declare a variable using “Var” or “let” BUT there is an inconvenient, don’t worry, I will give you context.

Some years ago(2015) Javascript changed. Var had a global scope before 2015, BUT with ecmascript6 it changed because We can use “Let” to declare a variable.
You shouldn’t use “Var” because it is considered a bad practice , It can get you into a value reassignment problem.

Example:
letvsvar01.png

In this example I created a variable using “var” and printed on console three times to show you this…

letvsvar02.png

You can see that the variable var was added to window’s global object.

letvsvar03.png

letvsvar04.png

BUT if you create a variable using LET, you won’t have that problem, and the variable LET could be a local or global variable.

letvsvar05.png

I hope I have helped you by solving this question and You have saved time.
I look forward to your comments, have a nice day. =D

Discover and read more posts from Ricardo Garrido
get started
post comments1Reply
Abhishek Chokra
3 years ago

I would like to include ‘const’ as well in this post with read only properties.