0

FreeCodeCamp – ES6 – Compare Scopes of the var and let Keywords

When you declare a variable with the varkeyword, it is declared globally, or locally if declared inside a function.

The letkeyword behaves similarly, but with some extra features. When you declare a variable with the letkeyword inside a block, statement, or expression, its scope is limited to that block, statement, or expression.

For example:

var numArray = [];
for (var i = 0; i < 3; i++) {
numArray.push(i);
}
console.log(numArray);
// returns [0, 1, 2]
console.log(i);
// returns 3

With the varkeyword, iis declared globally. So when i++is executed, it updates the global variable. This code is similar to the following:

var numArray = [];
var i;
for (i = 0; i < 3; i++) {
numArray.push(i);
}
console.log(numArray);
// returns [0, 1, 2]
console.log(i);
// returns 3

This behavior will cause problems if you were to create a function and store it for later use inside a for loop that uses the ivariable. This is because the stored function will always refer to the value of the updated global ivariable.

var printNumTwo;
for (var i = 0; i < 3; i++) {
if(i === 2){
printNumTwo = function() {
return i;
};
}
}
console.log(printNumTwo());
// returns 3

As you can see, printNumTwo()prints 3 and not 2. This is because the value assigned to iwas updated and the printNumTwo()returns the global iand not the value ihad when the function was created in the for loop. The letkeyword does not follow this behavior:

‘use strict’;
let printNumTwo;
for (let i = 0; i < 3; i++) {
if (i === 2) {
printNumTwo = function() {
return i;
};
}
}
console.log(printNumTwo());
// returns 2
console.log(i);
// returns “i is not defined”

iis not defined because it was not declared in the global scope. It is only declared within the for loop statement. printNumTwo()returned the correct value because three different ivariables with unique values (0, 1, and 2) were created by the letkeyword within the loop statement.


Fix the code so that ideclared in the if statement is a separate variable than ideclared in the first line of the function. Be certain not to use the varkeyword anywhere in your code.

This exercise is designed to illustrate the difference between how varand letkeywords assign scope to the declared variable. When programming a function similar to the one used in this exercise, it is often better to use different variable names to avoid confusion.


Solution:

function checkScope() {
“use strict”;
    let i = “function scope”;
    if (true) {
    let i = “block scope”;
    console.log(“Block scope i is: “, i);
    }
    console.log(“Function scope i is: “, i);
    return i;
}

Definition – What does Web Programming mean?

Web programming refers to the writing, markup and coding involved in Web development, which includes Web content, Web client and server scripting and network security. The most common languages used for Web programming are XML, HTML, JavaScript, CSS and PHP. Web programming is different from just programming, which requires interdisciplinary knowledge on the application area, client and server scripting, and database technology.

At some point most web developers will be in the position of having to either provide hosting for their clients’ websites, or they will need to know which web hosts to recommend to their clients. Web hosting services store and activate all the files your website needs in order to function and to be active. Some of the best web hosting companies are: HostGator, GoDaddy, Bluehost, iPage, InMotion Hosting, HostFav, DreamHost, Interserver, Namecheap, 1 & 1, Fasthosts, Hostinger, GreenGeeks, Justhost, SiteGround, Host1Plus, eUKhost, Arvixe, FatCow, HostMonster and more.

Web Programming Degree

If you have a passion for connecting people through technology, our online Web Programming degree will provide you with in-demand skills to do so in a growing industry. In our program, you’ll become proficient in Java, Microsoft Azure, Google Developers Training, Web development and cloud computing—all of which help you prepare for various types of web programming roles, such as web developer, front-end developer, back-end developer or web app developer.

Through online Web Development courses, you’ll build your own professional portfolio for your future employers.

Related Post

Matlab

Leave a Reply

Your email address will not be published. Required fields are marked *

22 − = 20