What Are Variables in JavaScript?
Variables are used to store data such as names, numbers, and other values.
JavaScript provides three keywords to declare variables:
varletconst
1. var
var is an older way to declare variables in JavaScript.
var name = "John";
console.log(name);
Output:
John
We can also reassign the value of a var variable.
var age = 20;
age = 25;
console.log(age);
Output: 25
2. let
let is used when we want to declare a variable whose value may change.
let score = 50;
score = 80;
console.log(score);
Output: 80
Unlike var, let cannot be declared again in the same scope using the same name.
3. const
const is used when we don't want to reassign a variable.
const country = "India";
console.log(country);
Output: India
We cannot reassign country to another value. However, if a const variable stores an object or array, its contents can still be modified.
Top comments (0)