Use of the Javascript keywords var, let, and const in a variable declaration
Dennis Ng'ang'a Mbugua
Posted on March 20, 2023
Pre-requisites
This article is targeted at beginners who want to learn about keywords, var, let and const in variable declaration.
Before diving into this article, make sure that you understand the
camelCase naming convention used in JavaScript.
Data in computer science refers to everything that the computer can use to make sense of. Undefined
, null
, Boolean
, text
, symbol
, int
, number
, and object
are the eight distinct data types that JavaScript supports.
For example, computers distinguish between numbers, such as 12
and strings
, "123”, which are collections of characters.
Also, computers can perform mathematical operations on numbers but not on strings.
In this article, we are going to learn about:
Table of contents
1.What is a variable?
2.variable declaration using var
3.Cons of using var keyword
4.using keyword let
5.Cons of using let keyword
6.using keyword const
7.Best practices of declaring variables
8.conclusion
What is a variable?
A variable is a storage location that allows computers to store and dynamically manipulate data. Variables, do this by using a label that points out to them rather than using data itself. Any of the eight data types highlighted above may be stored in a variable.
Variables are similar to the x and y variables we use in mathematics. Therefore, variables are the simple name we use to refer to data. However, computer variables differ from mathematical variables in that they can store different values at different times.
Variable declaration using keyword var
We tell JavaScript to create or declare
a variable by putting
keyword var
in Infront of our desired variable name.
for example, let us say we want a variable called myName:
``Put the
varkeyword in Infront of your variable name like this,
myName`. Variable names can be made up of numbers, letters, and $ or _, but may not contain spaces or begin with a number.
var myName;
var myName creates a variable called
for example
var my Name -> !!!NOT ALLOWED
var 27myName -> !!!NOT ALLOWED
Cons of using the var
keyword
One of the biggest problems of using the keyword var
is that you can easily overwrite variable declarations.
var name = "Dennis";
var name = "Mbugua";
console.log(name);
In this above example, name
is originally declared as Dennis’. The variable name
Mbuguaoverwrites
Dennisand the console logs out
Mbugua`.
In a small application, you might not run into this type of problem. But as your codebase becomes larger, you might accidentally overwrite a variable that you did not intend to. Because this behaviour does not throw an error, searching for and fixing bugs becomes more difficult.
Using the let
keyword
In Es6, a major update in JavaScript, the let
keyword was introduced, which helps to exponentially solve this var
problem.
let name = "Dennis";
let name = "Mbugua”;
The error will be thrown onto the browser console. ‘let` keyword allows variables with the same name to be declared only once.
Advantages of using the let
keyword in a variable declaration
The let
keyword does not have major problems when compared with the var
keyword. As mentioned previously, let does not allow different variables to have the same name.
Using the const
keyword in the variable declaration
The keyword let
is not the only new way to declare variables. In Es6
, you can declare variables using the const
keyword.
The const
keyword has all the properties that the let
keyword has, with a bonus that, variables declared using const
are read only’. They are a constant
value. This means that variables declared with the
const` keyword cannot be reassigned once declared.
const met = "Cat";
myPet = "Dog";
The above code will throw an error in the browser console because a read only
variable is reassigned to a new value. You should always name variables that you don’t want to reassign using the const
keyword. This helps when you accidentally attempt to reassign a variable that you want to remain unchanged.
Best practices for declaring variables
*NOTE: * It is common for most developers to name immutable variables (which means can never be resigned once declared).
for example, const MYNAME
.MYNAME variable is immutable and is in uppercase letters. This allows another person reading your code to identify the variable as read-only.
Conclusion
In JavaScript, data is anything that can be manipulated dynamically. Computers allow data storage using variables.
Variables are declared by putting either keywords Var’, ‘let
or const
in Infront of your variable name. When declaring a variable, make sure you use the right keyword appropriately.
Happy coding Written by Dennis Mbugua. Developer and Writer
Follow me on Twitter link
Follow me on GitHub link
Posted on March 20, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
March 20, 2023