-
Notifications
You must be signed in to change notification settings - Fork 0
03 Variable
JavaScript variables are containers for storing data values.
- Name must start with a letter (a to z or A to Z), underscore( _ ), or dollar( $ ) sign.
- After first letter we can use digits (0 to 9), for example
num1 - JavaScript variables are case sensitive, for example
xandXare different variables.
- Variables can be declared using let, const, var
- Now days
varis not used and mostlylet&constare used. - We don't have to specify the data type while declaring the variables.
- We can assign any value to the variables, means
let xcan be assignedstringornumberetc.
The value is assigned once and can't be changed.
//This will throw error as the value of constant can't be changed.
const x=6;
x=7;
console.log(x);
//This will throw error as the constant must be initialized at the time of declaration
const y;
y=8;
console.log(y)
//This will work fine.
const z=10;
console.log(z)var is not scoped to block. It can be either global scoped or function scoped.
//This will print the value of i as 10
for(var i = 0; i < 10; i++){
console.log(i);
}
console.log(i);However let is block scoped. so we should always prefer that.
//This will throw error as there's no life of i outside of the for loop.
for(let i = 0; i < 10; i++){
console.log(i);
}
console.log(i);One major difference is let variables can't be redeclared in the same scope, however var variables can be redeclared.
//This will throw error
let a=1;
let a=2;
console.log(a);
//This will work fine.
var a=1;
var a=2;
console.log(a);One catch is that for let variable, if we keep the scope as global and local then the variable can be redeclared as that will be considered two separate variables.
//Works fine
let a=8;
function fun1(){
var a=2;
console.log(a);
}
fun1()- var declarations are globally scoped or function scoped while let and const are block scoped.
- var variables can be updated and re-declared within its scope; let variables can be updated but not re-declared; const variables can neither be updated nor re-declared.
- They are all hoisted to the top of their scope. But while var variables are initialized with undefined, let and const variables are not initialized.
- While var and let can be declared without being initialized, const must be initialized during declaration.
If anything is declared outside of the function, that becomes global variable and can be used by all the functions, however if anything is declared within a function /block then the scope of the variables are limited to the function/ block.
let x=10;
function one()
{
let y=20+x;
console.log(y)
}
function two()
{
//This will error as y is limited to function one only
//let z=30+y;
//This will work fine as x is global
let z= 30+x;
console.log(z);
}
one();
two();//We can declare the variables first and then assign the values
let a;
a=5;
console.log(a);
var b;
b=6;
console.log(b);
//Or we can declare and assign values at the same time
let a=8;
console.log(a);
//We can assign any value to a variable. means we can assign string and then number to a variable.
let c;
c=30;
console.log(30);
c="Thirty";
console.log(c);