JavaScript Variables. What to choose? Var, let or const? And why const?

JavaScript Variables

Once there was no dispute over how to declare variables, there was only var. However, since the coming of ES 6 things complicated, but why?

Var made problems from the start, most importantly because there was no differentiating between constant and variable and the biggest problem for programmers was the range of var variables.

So, what to choose? Let or const?

In the case of let and const range of variables is from bracket to bracket, which solves most problems, but to choose correctly you have to dig deep into the purpose of a declaration. Keyword let, allows us to create a variable in which we can change reference, unlike const. Const additionally forbids us from assigning variables again. What that entails is that whenever we want to declare a simple type (string, boolean, number, etc.), which we would want to change in the future, we should use let. When we would want to create a constant value, we use const.

What about reference type?

In those we always use const. The change of the value of a field in an object or the value of an array element will not change the reference. It is one of the most common mistakes made by starting programmers. When they make an array or an object, they declare using let, because they will want to change values inside. They couldn’t be more wrong. Keyword const, as mentioned before is used to assign reference, meaning to point to a specific “object” in memory. We cannot assign a new “object “to const but we may edit it (add/delete/edit elements of arrays and objects).

Why is it best to use only const?

The answer is simple, it is way easier to manipulate code. When we use a lot of variables declared using let, often we have to check step by step if something is not being overwritten somewhere. When we use const, every next attribution is considered an error by our beloved compiler and shown with a beautiful red caption.

Someone might ask, “Then how am I supposed to assign something to the const variable when I don’t yet know what is supposed to be there”.

Content

Got a project?

Let's talk!

__wf_zastrzeżone_dziedziczyć
AI
Secure AI - Advantages
arrow icon
7.12.2024
2 min read
Technologies
What is AWS?
arrow icon
4.1.2024
2 min read
Technologies
What is HTML?
arrow icon
3.21.2024
2 min read
Technologies
What is TypeScript?
arrow icon
3.20.2024
3 min read
Technologies
What is PHP?
arrow icon
3.19.2024
1 min read
Technologies
What is Swift?
arrow icon
3.18.2024
5 min read
Technologies
What is Kotlin?
arrow icon
3.16.2024
4 min read
Technologies
What is JAVA?
arrow icon
3.13.2024
2 min read
Technologies
What is React Native?
arrow icon
3.13.2024
3 min read
Technologies
What is React.js?
arrow icon
3.13.2024
2 min read
Technologies
What is Node.js?
arrow icon
3.13.2024
1 min read
Technologies
What is JavaScript?
arrow icon
3.13.2024
1 min read
Knowledge hub
What is a fullstack developer?
arrow icon
3.13.2024
1 min read
Knowledge hub
What is frontend?
arrow icon
3.13.2024
2 min read
Knowledge hub
What is backend?
arrow icon
3.13.2024
2 min read
IT
How to get started in IT?
arrow icon
3.6.2023
7 min read
IT
WEB3 - What is it? Introduction
arrow icon
2.21.2023
4 min read
UX/UI
UX Design - a guide for programmers
arrow icon
1.18.2023
4 min read
Business
Team Augmentation- Benefits!
arrow icon
1.4.2023
11 min read
Business
How to choose a programming company?
arrow icon
12.22.2022
8 min read
IT
How do programmers test?
arrow icon
12.18.2022
2 min read
IT
How to find good programmers?
arrow icon
12.15.2022
4 min read
Startup
What is a startup?
arrow icon
12.7.2022
7 min read
Code
Rust is the future of Server Side
arrow icon
12.1.2022
1 min read
IT
How to make a mobile app?
arrow icon
11.30.2022
5 min read
Business
How to work in various time zones?
arrow icon
11.27.2022
7 min read
Business
Where to Invest money in 2021?
arrow icon
10.31.2022
4 min read
IT
IT outsourcing – what is it?
arrow icon
10.25.2022
4 min read
Code
Why am I NOT a fan of TypeScript
arrow icon
10.23.2022
3 min read
Code
React Basics - State and useState
arrow icon
10.18.2022
5 min read