Var, let and const
Akande Olalekan Toheeb
Posted on February 23, 2022
Hi peeps 👋! I'm here again with a shot which will explain to you, the difference between var
, let
and const
in creating a JavaScript variable.
I am Akande Olalekan Toheeb, a Nigerian self-taught developer who is passionate about solving problems and helping others with the easiest way in problem solving.
Let's get started 😍!
Overview
Do you know what a Variable is ?
In JavaScript, A Variable is a way of storing and keeping track of values so they can be later used in the program. This definition is almost the same in all programming language.
Nearly all program runs on data and that is why the usage of variable is very important in writing codes.
A variable can store all kinds of data types which includes: Strings, Numbers, Arrays, Objects e.t.c
How to declare a Variable
There are three methods:
- The first way is to use the
var
keyword which is the most common method - Using the
let
keyword is the second - Using the
const
keyword emerges the last method in creating a variable.
Syntax
let variableName = '';
Some tips in Declaring Variable Names
Giving your Variable a name that corresponds to its value will be of great help when writing a code. This is because you won't have to stress yourself when next you need to use the stored in the variable. Below are some other tips in setting the name:
- A variable is case sensitive so you have to very be careful while naming, name is not Name.
- Always use the camelCase when declaring variables.
In camelCase, multi-word variable names have the first word in lowercase and the first letter of each subsequent word is capitalize. e.g
var aMultiWordVariableName;
- Do not begin a variable name with an underscore.
- Do not use variable names that consist of a single character. Short variable names are only allowed for loop counters.
Examples:
var name= "Akande Olalekan Toheeb";
let authorAge = 22;
const gender = "Male";
Difference between var
, let
, const
The three methods of declaring variables have their differences and this is explained in this section.
One of the biggest problems with declaring variables with the var
keyword is that you can easily overwrite variable declarations.
For example:
var name = "AKANDE";
name = "Olalekan Toheeb";
console.log(`My name is ${name}`);
In the above lines of code, the code will print My name is Olalekan Toheeb to the console. But why 😮???
This simply because the value of the variable has been overidden and this happened in the second line of code.
A keyword called let
was introduced in ES6, a major update to JavaScript to solve this issue.
Keyword let
is very similar to var
but it doesn't allow overidden of the value. The value can only be added to with the addition operator +
. e.g
var name = "Akande";
name = name + "Olalekan Toheeb"
console.log(`My name is ${name}`);
This will print My name is Akande Olalekan Toheeb to the console.
const
has all awesome features that let
has, with an added bonus that variables declared with const
are read-only. They are a constant value, which means that once a variable is assigned with const
, it cannot be override nor reassigned.
const name = "AKANDE";
console.log(`My name is ${name}`);
name = "Olalekan Toheeb";
console.log(`My name is ${name}`);
The above code will print My name is AKANDE to the console and then an error. This is because of the const
used in the declaring the variable.
Thank you for reading, have a nice day!
Your appreciation is my motivation 😊 - Give it a like
Posted on February 23, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.