DEV Community

Piyush Panchal
Piyush Panchal

Posted on

const vs. let

In JavaScript, There are three ways of declaring a variable, why though? How are they different? const cannot be redeclared. therefore it will have one constant value, and can't be changed. For Example-

const number = 1

number = 2
//Gives an error (const cannot be redefined.)
Enter fullscreen mode Exit fullscreen mode

Whereas let can be changed anytime. For example-

let num = 2

num = 5

//Value changes to 5 with no errors.
Enter fullscreen mode Exit fullscreen mode

Thanks for reading.
Hope I helped :D

Discussion (2)

rsa profile image
Ranieri Althoff • Edited on

It will not error since you are not redefining number in the first case. I think you meant number = 2 instead (in both cases).

programmerpiyush008 profile image
Piyush Panchal Author

oh yeah thanks for telling