The JavaScript Variables var, let, and const

Kelly M.
2 min readNov 10, 2019

Did you know that var, let and const have different abilities? Let’s dissect these JS variables one by one.

1. var

var mySuperhero= 'The Flash';

Let’s dig into the anatomy of a JS variable first. Like let and const, var is a declaration. The thing next to the declaration is the identifier. Identifiers must begin with a letter and are case sensitive. The case sensitivity here being camel case. Identifiers give us the opportunity to be as descriptive as we’d like. The = is the assignment operator. It is not equals to, it means to assign the value of. Here, ‘The Flash’ is our value. Values can be strings like ‘The Flash’ is, numbers, and objects.

The var has function scoped, can have duplicate identifiers, and its value is mutable, or changeable. So for example, Below I changed mySuperhero to ‘Supergirl’ using the same identifier, which is fine because remember we can have duplicate identifiers with var.

var mySuperhero= 'Supergirl';

2. let

let mySuperhero= 'The Arrow';

The variable let is block scoped. It cannot have duplicate identifiers, but its value is still changeable.

3. const

const mySuperhero= 'Batgirl';

The const variable is block scoped and it cannot have duplicate identifiers, just like let. However, unlike let and var, its value is immutable. We cannot change it.

To summarize their different abilities see table below.

--

--