Type Here to Get Search Results !

JavaScript var vs let

var ---VS--- let


var 

This keyword, which defines a variable globally, or locally to an entire function regardless of block scope.

function test(){
for (var i = 0; i < 10; i++) {
    // some code
    console.log("In block scope"+ i); // output: 0 to 9
}
    console.log("In local scope" + i); // output: 10
}

let

It allows you to declare variables that are limited in scope to the block, statement, or expression on which it is used.

function test(){
for (let i = 0; i < 10; i++) {
    // some code
    console.log("In block scope"+ i); // output: 0 to 9
}
    console.log("In local scope" + i);   //Uncaught ReferenceError: i is not defined
}

Post a Comment

1 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Top Post Ad

bogger post ad 1

Below Post Ad

bogger post ad 2

ezoic

Ads Section