Table of contents
Introduction
In declaring a variable in Javasccript, the var
keyword is utilized. However, the release of ES2015/ES6 in 2015 introduced two new keywords let
and const
. After the release of ES6 it is possible to utilize const, let or var when declaring a variable. Each of const, let and var is used for different purposes and could be necessary for specific situations based on the type of data you’re working with. The three keywords together provide the same fundamental functionality, but differ in the way that they provide it. var and let are re-assignable values, while const has limited mutability.
Today, in this article we'll examine the significant distinctions in const, let and var and when to use each to avoid stumbling into errors.
Const
This article will begin with const, which is likely to be one of the easiest to grasp. Const first came into JavaScript after ES6 was released in mid-2015. It has revolutionized the way variables function since then.
In general, const allows us to define constants (basically variables whose values can not change over time), Const is a block-scoped object. It means that any action you accomplish using const will be limited to what you can do with it. Const variable is only employed inside the blocks of code that is first created. That is, If you try to alter the value of a const variable, it will end with an error.
For example, the code below will end up in an error saying that, Uncaught TypeError: Assignment to constant variable.
const firstName = "Isaac";
firstName = "Newton"
console.log(firstName) //Uncaught TypeError: Assignment to constant variable.
this happens because the const variable was altered resulting to an error.
Use case: Don't declare variable with const
keyword only when knowing that a type of data remains constant but also when you’re not certain whether data will need to change or not, why? because using it as default helps you realize if a value mutates or not and diminishes the number of mutations, if you never had the need to change it, you know it is a constant.
Note: The reserved keyword const
only makes the variable itself immutable which means that if we define an object or an array using it we could still modify the object’s or array’s properties and values, for example:
Let's say we have a const variable of person
which is assigned an object as below:
const person = {
name: "Isaac newton";
}
IF we try a property of isAScientist
to the person
like this:
const person = {
name: "Isaac newton";
isAScientist: true;
}
We’ll still get an error with something like, Uncaught TypeError: Assignment to constant variable
, excellent, just what we wanted right?, hmm… but wait, what happens if instead, we do something like this:
const person = {
name: "Isaac newton";
}
person.isAScientist = true;
Now we didn't have an error because this behavior occurs because what we’re actually changing is the object’s property, we’re not changing the variable itself, this same principle can be applied to arrays so be aware next time.
let
let
is used to declare variable that can change over time in our code for example:
let job = "Teacher";
job = "Developer";
console.log(job); // Developer
In the above example we declare a variable with the keyword let
which we assigned the value of Teacher
to the job variable, after, we console it to the log which prints Teacher
as expected. Then we reassign our job
variable a value of Developer
when we print it the job
value automatically change to Developer
without any error showing that the let
keyword value can be assigned.
Note
Keywords let
or const
variable can only be declare once. if it is declare it again it will leads to error.
for example, based on the code above if you re-assign a value to job
using the let reserved word like this:
let job = "Teacher";
let job = "Developer";
console.log(job); // Uncaught SyntaxError: Identifier 'job' has already been declared
The error Uncaught SyntaxError: Identifier 'job' has already been declared
will show at the console.
var
Before the advent of ES6, the method by which we declared variables was using the keyword var
. Var
isn’t employed as frequently as it was with the start of Let and const; however, it isn’t completely eliminated from JavaScript and has its particular usages.
Var, as well as let, are identical in that you can change the value later. The primary difference between them is that Let is a block scope application, while the var is a scope of a function or global scope based on the place it’s declared. So if your var isn’t declared in the scope of any other function, it may be used in any other place within your code.
In addition, unlike, varlets you declare the var however many times you’d like, and there’ll be no errors to be thrown at you. So, the instance above using Let caused an error, while the example below won’t.
var job = "Teacher";
var job = "Developer";
console.log(students); // Developer
conclusion
This is a thing that’s easy to overlook if you aren’t careful, so you must ensure that you’re employing the proper syntax, based on whether you’re using const, lets or var to declare variables in javascript.