DEV Community

loading...
Cover image for What is the difference between var, let, and const declarations?

What is the difference between var, let, and const declarations?

ip127001 profile image Rohit Kumawat ・2 min read

It is the most common question asked in a JavaScript interview. var was the only option for variable declaration in Javascript before ES6 but after ES6 let and const were announced.
I have written three separate blogs each one describing thoroughly all 3 types of variable declarations. If you want to check the out before proceeding further:

Q.1) What is the difference between var and ES5 new declarations (let/const)?

var let/const
var can't create block scope let/const creates block scope
var can be re-declared let/const can't be re-declared
var declarations are hoisted and initialized with undefined let/const declarations are hoisted but are not initialized

Q.2) What is the difference between let and const?

let const
variable declared with let can be re-assigned. const declarations can't be re-assigned.
If the variable(declared with let) is not initialized with any value, JavaScript assigns undefined to it. If the variable(declared with const) is not initialized with any value, JavaScript throws an error.
let a; //a = undefined const a; //SyntaxError: Missing initializer in const declaration
let a = 10; a = 5; //a = 5 const a = 5; a = 10; //TypeError: Assignment to constant variable

Q.3) Can you change the value of a variable declared with const?

Yes, only if it is a reference type.

const declaration can't be assigned again that doesn't mean you can't change the existing value.
Take the example of assigning an array to a const declaration. We can't assign another array but we can change the value of the existing array because the reference is not changing.

const arr = [1,2,3];
arr.push(4);
console.log(arr); //[1,2,3,4]

const arr = [1,2,3];
arr = [4,5,6];
TypeError: Assignment to constant variable
Enter fullscreen mode Exit fullscreen mode

So use let in such cases where array values will be changed in the future like in the above example to avoid the confusion created by the meaning of const.
You can use const anywhere if the values will not be changed in the future.


Conclusion:

  • In my current coding style I don't use var.
  • I use let where values can be changed in the future.
  • I use const for cases where values will not be changed like imports, exports, constant values, etc.

Happy coding!
If you want to wanna talk about tech and F.R.I.E.N.D.S we can connect on Twitter.

Discussion (6)

pic
Editor guide
Collapse
sunitk profile image
Sunit Katkar

I am relatively new to TypeScript but have been using Javascript since it was first released by the Mozilla browser years ago. I too use 'let' instead of 'var' and 'const' just like a static final variable in Java.

Collapse
ip127001 profile image
Rohit Kumawat Author

Cool, Thanks for reading :)

Collapse
bigpresh profile image
David Precious • Edited

In Q2,

const a = 5; a = 10; SyntaxError: Missing initializer in const declaration

... I think you mean TypeError: Assignment to constant variable there :)

Collapse
ip127001 profile image
Rohit Kumawat Author

Thanks for reading the article and observing the mistake.

Collapse
asif0228 profile image
Asif Uddin

Informative

Collapse
ag251994 profile image
abhishek gupta

Very well written. Thanks