If you have any query feel free to chat us!
Happy Coding! Happy Learning!
In ES6, the let
keyword was introduced, which has block scope. This means that variables declared with let
are only accessible within the block they are declared in. One of the main advantages of let
over var
is that it can be used in for loops to create block-scoped loop variables.
Consider the following example:
cssCopy code
for (let i = 0; i < 5; i++) {
console.log(i);
}
console.log(i); // throws an error because i is not defined
In this example, the let
keyword is used to declare the loop variable i
. Because let
has block scope, i
is only accessible within the for loop block. Therefore, the console.log(i)
statement outside of the loop block will throw an error.
On the other hand, if var
had been used instead of let
, the loop variable i
would have function scope, which means that it would be accessible outside of the for loop block:
cssCopy code
for (var i = 0; i < 5; i++) {
console.log(i);
}
console.log(i); // outputs 5
In this example, the loop variable i
is declared using var
, so it has function scope. Therefore, the console.log(i)
statement outside of the loop block will output the value of i
, which is 5
.
Comments: 2
I am not able to access videos from second class and further. I have already completed first class
When will I get my course?
Now, Your query was resolved.