What is a Variable?

A variable is like a box where you can store information.
You give the box a name so you can use the information later.
Variables can store things like numbers, words, or true/false values.
How to Create a Variable?
In JavaScript, you create a variable using one of these keywords:
var
let
const
Example: Create a variable with var
let name = "Alice";
console.log(age);
Here,
name
is the variable."Alice"
is the value stored inside the variable.
Now you can use name
to show or work with "Alice"
.
20
Difference Between let
, var
, and const
let
— you can change the value later.var
— old way to create variables, try to uselet
instead.const
— cannot change the value once set (constant).
Example: Changing a variable with var
let score = 10;
score = 15;
console.log(score);
Example: Constant with const
const pi = 3.14;
pi = 3.15; // This will give an error!
Summary :
Variables store information you want to use later.
Use
var
orconst
to create variables.Use
var
if you want to change the value.Use
const
if the value stays the same.
Subscribe to my newsletter
Read articles from Hamza Raza directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
