Var, let and Const in Javascript.
Variable
A variable is a “named storage” for data. We can use variables to store goodies, visitors, and other data. In JavaScript, there are different ways of creating variables which includes var, let, and const. Here, we will talk about the scope and difference between these three ways.
Var
The var
statement declares a function-scoped or globally-scoped variable, optionally initializing it to a value
var
declarations, wherever they occur, are processed before any code is executed. This is called hoisting and is discussed further below.
The scope of a variable declared with var
is its current execution context and closures thereof, which is either the enclosing function and functions declared within it, or, for variables declared outside any function, global. Duplicate variable declarations using var
will not trigger an error, even in strict mode, and the variable will not lose its value, unless another assignment is performed.
Here we created and called a function, printName, that will print the value of the name var, Madison
. You’ll see this printed in your console.
Because the var was created outside of the function, it is globally scoped. This means that it is available anywhere in your code, including inside of any function. This is why our function, printName, has access to the name var.
Let’s create a variable that is function-scoped. This means that the variable is only accessible inside the function it was created in. This next example will be very similar to the code above, but with a different placement of the variable.
let
The let
declaration declares a block-scoped local variable, optionally initializing it to a value.
let
allows you to declare variables that are limited to the scope of a block statement, or expression on which it is used, unlike the var
keyword, which declares a variable globally, or locally to an entire function regardless of block scope.
However, let
differs from const
in a fundament way. Variables declared with the let
keyword can be redeclared, while variables created with the const
keyword cannot. Going over an example we have:
By using the let
keyword, we’ve updated our variable to point to the value of true
as we wanted to. Sometimes in programming we’ll want to update our variable depending on certain data that we receive. We can use let
to do this.
Const
The const
keyword was also introduced in the ES6(ES2015) version to create constants. For example,
Once a constant is initialized, we cannot change its value.
A constant is a type of variable whose value cannot be changed.
Also, you cannot declare a constant without initializing it. For example,
Note: If you are sure that the value of a variable won't change throughout the program, it's recommended to use const
.
Difference between var, let and const
Var
The scope of a var variable is functional scope
It can be updated and redeclared into the scope
It can be declared without initialization
It can be accessed without initialization as it's default value is "undefined".
Hoisting done, with initializing as "default value".
Let
The scope of a let variable is block scope
It can be updated but cannot be redeclared into the scope
It can be declared without initialization
It cannot be accessed without initialization otherwise it will give "reference error".
Hoisting is done but not initialized.
Const
The scope of a const variable is block scope
It cannot be updated or redeclared into the scope
It cannot be declared without initialization
It cannot be accessed without initialization, as it cannot be declared without initialization.
Hoisting is done but not initialized.
Subscribe to my newsletter
Read articles from Jennifer Muofunanya directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Jennifer Muofunanya
Jennifer Muofunanya
Hey😊... Welcome to my profile. I am Chioma Jennifer Muofunanya. A passionate developer skilled in HTML, CSS, JavaScript, React, Node.js, MongoDb and Express. Let's connect and create something amazing together