let - Keyword in JavaScript (Block scope)

Before ES2015  javascript variable has only two scopes i.e. Global Scope and Function Scope.

but in ES2015 they introduced the let keyword. this keyword provides Block scope variables. Block scope means Variables declared inside a block {} cannot be accessed from outside the block.

For Example :

function getPersonDetails(){

     var name ="Nilesh";

      let newName = "Nil"

}

console.log("Print variable with var "+ name);    // you can access name variable outside function

console.log("print variable value with let"+newName);  // you can not access newName Variable outside.


In the above example getPersonDetails is a function that contains two variables "name " and "newName" with the keyword var and let. hear you can access the name variable which is declared with var keyword outside of the function. but you can not access the newName variables which is declared with the let keyword outside of the function.

 





Comments

Popular posts from this blog

public static void main(String [] args) - Full Explanation

Coding Problem 3 - Weather Observation Station 7 [HackerRank - SQL]

Date Range Picker In Angular 10