Skip to content
Related Articles

Related Articles

JavaScript | Let
  • Last Updated : 05 Apr, 2021
GeeksforGeeks - Summer Carnival Banner

let is a keyword used to declare variables in javascript that are block scoped. There are three ways you can declare variables in javascript let, const, var(old). ES6 or ES2015 defines new keywords to declare variables in javascript. 
 

Syntax:

 

let x=1, y=3, z=4;

 

Block Scope:



A variable can be either of global or local scope. A global variable is a variable declared in the main body of the source code, outside all functions, while a local variable is one declared within the body of a function or a block. 
 

Global Scope:

 

javascript




let num=10;
console.log(num);
function fun(){
    console.log(num);
}
fun(); // calling the function

Output: 
 

 

Function Scope:



 

javascript




function fun(){
    let num=10;
    console.log(num);
}
fun(); //  calling the function
console.log(num);

Output: 
 

 

Block Scope:

 

javascript




{
    let x=23;
    console.log(x);
}
console.log(x);

Output: 
 

 

Redeclaring Variables in different blocks:

 

javascript




let x=77;
{
    let x=23;
    console.log(x);
}
console.log(x);

Output: 
 

 

Redeclaring Variables in same blocks:

 

javascript




let x=77;
{
    let x=23; // legal
    console.log(x);
}
let x=67;// illegal
console.log(x);

Output: 
 

 

Does not support Hoisting:

 

javascript




x=12;
console.log(x);
let x;

Output: 
 

 

full-stack-img

My Personal Notes arrow_drop_up
Recommended Articles
Page :