Review - help me to make it more suitable


var count = 3;

for (var i = 0; i < count; i++) {
console.log(i + "for statement");

var condition = true;

while(condition) {
console.log("while condition is true");
condition = false;

var conditionTwo = false;

do {
console.log("do condition is false");
} while(conditionTwo);

My code is run , but is it correct totally or need to change something specially 'for'

i need better suggestion 'for' loop to make it more suitable


In this example we take advantage of one of the weaknesses of JavaScript known as data leakage. In JS up to current day ES6 standards only functions have what is called block scope Modern JS lets us scope variables to any block, not just a function. This will take some extra reading.

count = 3;
for (var i = 0; i < count; i++){
    console.log(i);             // 0 1 2 
while (i > 0){                  // 3 2 1
do {
    console.log(i);             // 0
} while (i);

The for loop accumulates the variable i up to 3, logging 0, 1 and 2.

The while loop loop takes that variable (which has leaked out of the for block) and counts it down from 3 to 0, logging 3, 2, and 1.

The do while loop executes one log out of i, 0, then terminates since 0 is not a truthy value.

We couldn't do this were it not for the variable i leaking out of each successive block. We could even log i once all the code has completed running.

console.log(i)        // 0

Bear in mind that I demonstrate this not as a best practice or some fancy feature. As noted above, it is a weakness, that's why ES6 introduced block scope and a new keyword, let. This will take some extra reading. Add it to your reading list and keep forging through the track. Don't let this interfere with your studies at this present time.


can i get some resourceful links or ebook about this thinks u hve replied to me ?


First I must correct the terminology. data leakage refers to a rather different concern across data bases and networks. The term we may be referring to is, memory leakage. This term surfaced with the following search:

Or, we might just settle on a nickname, leakage as refers to lack of block scope. Perhaps this article I uncovered will put my gibberish above in clearer light.

This is extra reading that should not take you away from the present studies. While the course does explain scope, it is the ES5 scope that is taught in this track. The code example above takes advantage of the fact that i is in global scope which allows it to permeate all the statements. For now, it is enough to understand how we used i above.