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:
varletconst
Example: Create a variable with var
let name = "Alice";
console.log(age);
Here,
nameis 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 useletinstead.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
varorconstto create variables.Use
varif you want to change the value.Use
constif the value stays the same.