Variable
Variable is a contener that stores data and save it to memoey location.
There are 3 types of variable’s in JavaScript var
, let
, const
Syntex:
const variable_name;
let x = 100
var y = 200
Keyword | Description |
---|---|
var |
Try to avoid using it because of the block scoping issue. |
let |
Introduced in ES6 (2015). Variables declared with let have block scope and must be declared before use. |
const |
Once declared, const variables cannot be reassigned or modified. |
Let’s see a Example:
const accountId = 4303;
let accountEmail = "demo@gmail.com";
var accountPass = "123456";
accountCity = "Kolkata"; // not a good practice
let accountState;
// console.log() is use to print in console;
console.log(accountId);
console.log(accountEmail);
console.log(accountPass);
console.log(accountCity);
console.log(accountState);
Output:
4303
demo@gmail.com
123456
Kolkata
undefined
Change data of variable:
accountEmail = "octaveweb@gmail.com";
accountPass = "123910";
accountState = "West Bangal";
console.log(accountEmail);
console.log(accountPass);
console.log(accountState);
Output:
octaveweb@gmail.com
123910
West Bangal
See some Error’s
const accountId = 4303;
console.log(accountId);
Output:
4030;
But if we that to assign
const accountId = 4303;
console.log(accountId);
accountId = 20; // ❌ Error: Assignment to constant variable.
Output:
Error: Assignment to constant variable.
Use
let
insted ofconst
if you know the value might change in futute