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

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.

Zmienne JavaScript

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ć
Business
Business
Profesjonalna aplikacja dla firmy - 10 wskazówek
arrow icon
5.12.2023
7 min czytania
Business
Business
Doradztwo IT - korzyści dla Twojej firmy
arrow icon
3.21.2023
6 mi czytania
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
Code
Code
Czy MobX to dobra alternatywa dla Redux?
arrow icon
2.1.2023
3 min czytania
UX/UI
UX/UI
UX Design - przewodnik dla programistów
arrow icon
1.30.2023
4 min czytania
Code
Code
Podstawy React’a - stan i hook useState
arrow icon
1.26.2023
5 min czytania
Startup
Startup
Startup - z jaką firmą IT współpracować?
arrow icon
12.20.2022
11 min czytania
Business
Business
Najlepszy kraj do outsourcingu IT
arrow icon
12.12.2022
4 min czytania
Business
Business
Jak zmienić firmę programistyczną?
arrow icon
12.2.2022
4 min czytania
Business
Business
Outsourcing IT- kompletny PRZEWODNIK!
arrow icon
11.28.2022
3 min czytania
Business
Business
Team Augmentation - Korzyści!
arrow icon
11.23.2022
9 min czytania
Business
Business
W co inwestować pieniądze w 2021 roku?
arrow icon
11.21.2022
3 min czytania
Business
Business
Praca w różnych strefach czasowych. Jak to działa?
arrow icon
11.18.2022
7 min czytania
Startup
Startup
Co to jest startup? (Nowoczesne Przedsiębiorstwo)
arrow icon
11.17.2022
6 min czytania
Business
Business
Co to jest outsourcing pracowników IT i ILE kosztuje
arrow icon
11.14.2022
5 min czytania
IT
IT
Jak stworzyć aplikację mobilną?
arrow icon
11.11.2022
5 min czytania
IT
IT
Jak testują programiści?
arrow icon
11.10.2022
2 min czytania
Code
Code
Rust to przyszłość Server Side
arrow icon
11.8.2022
1 min czytania
Code
Code
Dlaczego nie jestem fanem TypeScript
arrow icon
11.7.2022
3 min czytania
Business
Business
Jak wybrać firmę programistyczną? Czym się kierować?
arrow icon
11.6.2022
7 min czytania
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
We use cookies. By continuing to browse the site you are agreeing to our privacy policy.