+ 8
Var i is in a loop that adds 1 while i is smaller than 9. On the last iteration, where i = 9, it adds 1 for a last time and the loop ends. The "i++" part is executed once more after the condition is false.
3rd Feb 2020, 12:04 AM
Fernando Pozzetti
Fernando Pozzetti - avatar
+ 8
Basic for loop runs until the condition is met i=0; i < 10; i++ Inside the loop i will reach 9 Outside the loop i will be 10 You can test that with the following code: for (var i = 0; i<10; i++) { console.log("value of i inside loop: " + i); } console.log("value of i outside of the loop: "+ i); The following part of your code won't work: for (let j = 0; j<10; j++) { } console.log(j); Reason: let is block scoped and is "invoked" inside the curly brace and cannot be called outside of it You can fix this by making let scope to entire window (global? Sorry I'm a java nerd) //declare j outside of the for loop scope let j; for (j = 0; j < 10; j ++) { } console.log (j); Also a friendly reminder - for loops without body exist! (at least in java 😝) for ( var x = 0; x < 10 ; x++); //no body in this loop console.log(x); Output: 10
3rd Feb 2020, 12:09 AM
HNNX 🐿
HNNX 🐿 - avatar
+ 4
SainPro 🤝 It is still a good attempt to help answering in Q&A, because it pushes us to do revision / search to learn knowledge. Look forward to seeing more contribution from you 💪
3rd Feb 2020, 7:44 AM
Gordon
Gordon - avatar
+ 3
SainPro No, only const is not changeable. Variables declared with let keyword is changeable. The reason for ReferenceError is because variables declared with let is block-scoped. See this lesson which explained very well: https://javascript.info/var
3rd Feb 2020, 5:52 AM
Gordon
Gordon - avatar
+ 2
Because... 😊 while(i < 10){ // output i and THEN increment 1 console.log(i); i++; } output: 123456789 while(i < 10){ //increment 1 THEN print i++: console.log(i); } Output:12345678910
3rd Feb 2020, 12:22 AM
HNNX 🐿
HNNX 🐿 - avatar
+ 2
Console.log(i) or console.log(j) must be inside the loop , not outside the loop. Outside the loop, there is no scope of i and j
3rd Feb 2020, 6:05 PM
Anmol Kumar
Anmol Kumar - avatar
+ 1
console shoud be inside the loops
9th Feb 2020, 4:29 PM
Jagbir singh
Jagbir singh - avatar
0
Please I need more explanation on JS computed property names
4th Feb 2020, 2:11 PM
Mr smile muhammed
Mr smile muhammed - avatar
0
Help me please
31st Mar 2020, 10:16 AM
Алексей Шеденко