DEV Community

Viviane Urbano
Viviane Urbano

Posted on • Edited on

Brief explanation about let, const and var

In Javascript you can use let, const or var to declare variables. But, before getting into this topic, let's understand about scope in Javascript.

The Javascript scope it is the execution context where variables are visible and accessible. In this programming language there are 3 scopes:

  • Global scope: The default scope for all code executed in script mode.
  • Module scope: The scope of code running in module mode.
  • Function scope: The scope created with a function.

Regarding our main topic here: 'how to declare variables', there is an additional scope:

  • Block scope: variables declared with let or const belong to this scope. The block scope is created with a pair of curly braces (a block). An 'if statement' for example creates a block scope
let y = 1;
if (true) {
  let y = 2;
}
console.log(y);
// expected output: 1
Enter fullscreen mode Exit fullscreen mode

Being less technical, I once read this comparison about scopes:

- Block scope: when you are speaking inside your room, so only you and whoever is there will hear (ie: only variables within the block scope have access to their values).

- Function Scope: when you're screaming inside your house, so everyone inside the house will hear you (ie: you can work with all variables within the function's scope). As this is a less technical comparison, here we can say that the Module scope works in the same way.

- Global scope: when you are screaming at the top of your lungs outside your house, then whoever is in your house and your neighbours will hear you (ie: if you declare a variable globally, you can access its value from any point in your program.

But why is important to understand about scope?

I would say it's because you need to understand how variables work, how you can access them, retrieve their values. If you don't pay attention to rules like these, you may fail to build your program.

Well, having said that, let's now look at our topic: how to declare variables and what kind of declaration to use.


1️⃣ LET
We use the let keyword to declare variables whose value can change later during the execution of our program.

let myVariable = 10
console.log(myVariable)
myVariable = 'Now has become a string'
console.log(myVariable)
Enter fullscreen mode Exit fullscreen mode

2️⃣ CONST
On the other hand, we use the const keyword to declare variables that should not change at any time in the future. This means that the value in a const variable cannot be changed.

const myVariable = 10
console.log(myVariable)
Enter fullscreen mode Exit fullscreen mode

If you mistakenly try to assign a new value to a const, you will face this kind of error message:

🔺 TypeError: Assignment to constant variable.
Enter fullscreen mode Exit fullscreen mode

To resolve this error, do not assign new values ​​to const variables.

Another important aspect is that as const is an immutable variable, you cannot declare it empty, with no values. Const declarations must always be initialized.

const myVariable; // THIS IS WRONG
Enter fullscreen mode Exit fullscreen mode

The above statement is not legal.

The main rule about const is that it is an immutable variable. BUT, here are 2 more highlights:

  • if you declare a const for a primitive type, you really can't assign a new value to a const.
  • if you declare an object as a const variable you can assign new values ​​to the properties of that object. This is possible because objects are referenced values, not primitive values.

3️⃣ VAR
This is an old-fashioned way of declaring variables. It predates ES6 (ECMAScript 6). It works similarly to let; var allows you change the value assigned to the variable.

Here it is important to note that var is either a function scoped or a globally scoped variable. And what does that mean? This means that if you declare a var variable, you can access its value at any point in your program and this can cause a lot of problems when debugging your code.

A valuable tip is to NEVER USE var to declare variables. This is the old way of declaring variables and it will continue to exist because JavaScript is backwards compatible. This means that once something is accepted as valid JS, there will not be a future language change causing code to become invalid JS.


🥸 If you find any English mistakes or nonsense, please let me know and leave a comment below. In addition to technology, I am interested in becoming a better English writer.

Top comments (0)