Contrasting features amongst these JavaScript reserved words mentioned below.
let, const and var.
As essential as it is to make declarations in our source codes, it is also very important to understand reserved words used for declarations and apply them in the best context where they function optimally.
Today, I will be sharing crucial points on these three reserved words below, used for variable declarations;
let
.const
.var
.
Before we delve into variable declaration concepts, Let me give you a bit of context about variables.
Variable It is simply a storage container located in the computer's memory, It is used to hold values/data assigned to the storage container(Variable) that would eventually be referenced later on while programming.
Now we can talk about these reserved keywords.
1. let:
let
is a modern variable declaration keyword.
2. const:
const
is used to declare constant variables, the variable you do not intend on changing later.
3. var:
var
is an old-school variable declaration keyword, it is similar to the let
keyword, but has slight significant differences.
Now, to contrast these declaration keywords;
First off, let
and const
are very similar, they function in the same scope of operation (within a code block), but the only difference is that const
is used to declare an unchangeable variable.
Next up, var
and let
are almost the same, but slightly different.
1. var
has now block scope, i.e; the variable is either function-scoped or global scope. They can be used outside their declaration block unlike let
that can only be inherited or shared with an internal block. So if you declare a variable with var
inside a block that's situated inside a function, the variable is only restricted to the functions block.
e.g
function sayHello()
{
if (true)
{
var phrase = 'Hello Victor.'
}
alert(phrase); //-> Hello Victor.
}
sayHello();
alert(phrase); //-> Reference Error: phrase not defined.
2. var
tolerates redeclaration of the same variable name. So unlike the let
keyword, var
allows you to redeclare a variable with the same name more than once.
Note: Each time you redeclare a variable with the same name, you're getting rid of the initial value/ data assigned to the variable at that point of declaration.
e.g
function sayHello()
{
var name = 'Victor';
console.log("Hello " + name + "."); //-> Hello Victor.
var name = 'Verike';
console.log("Hello " + name +"."); //-> Hello Verike.
}
sayHello();
3. var
can be declared below their usage line/ statement.
e.g
function sayHello()
{
phrase = "Hello there, I'm Victor"; // Variable assignment.
alert(phrase); //Variable usage.
var phrase; // Variable Declaration.
}
sayHello():
This characteristic is called hoisting or Raising.
Note There are side effects to this feature and we will be delving into it in subsequent variable series.
Thank you for taking out the time to read through this article. I hope you were able to assimilate my points on var
and let
.
Feel free to check out my previous article on Git and Version Control.