Javascript: Var, Let, Const

Carlos Pineda
1 min readMar 29, 2020

--

When I first started learning Javascript, I learned to use var to declare a variable and used it until I learned about let and const. Which led me to wonder the difference between the three. I learned that there are two aspects in which they differ: declaration/assignment and scope

Declaration/assignment:
When you declare a variable and assign it a value, the type you choose (or don’t choose) will depend on what you want to do with it later on.

  • VAR — can be declared again and be assigned another value
  • LET —cannot be declared again, can be assigned another value
  • CONST — cannot be declared again, cannot be assigned another value

Scope:
If you don’t quite understand scope yet, I suggest you read another blog post where I break it down quickly.

  • VAR — can be accessed anywhere in a functional , regardless if declared inside a block
  • LET — can only be accessed within the same block
  • CONST — can only be accessed within the same block

Since their introduction with ES6, it’s generally a good idea to use Let and Const before Var. Although seeing Var is likely a sign pre-ES6 legacy code.

--

--