DEV Community

Chiemezie
Chiemezie

Posted on

Difference between let, const, and var (JavaScript)

A Variable

Variables are used to store data (for example age, number, store items, person, etc...). We can declare variables in JavaScript with let, const, and var.

Let

Let is used to store a value that needs to change in our code. Example age

let age = 30;

//On my birthday
age = 31;
console.log(age); //expected value 31
Enter fullscreen mode Exit fullscreen mode

Const

Const is used to store values that don't need to change(immutable variables). Example birthyear

const birthyear = 1992;

//if we try to reassign birthyear we get an error
birthyear = 2000;
console.log(birthyear); //expected value error
Enter fullscreen mode Exit fullscreen mode

Var

Var is just like let meaning you can assign and reassign the value but is an old-school way of using Let.

var message = "Hello";
Enter fullscreen mode Exit fullscreen mode

Top comments (0)