That’s what I was talking about above. Let me make my comments a bit clearer.
Consider this loop.
for (let i = 0; i < 5; i++)
console.log(i);
This works fine because we are only executing one line. If there are not enclosing {}, then the loop will only run the ‘next line’ of code. Since whitespace is not syntatically meaningful to JavaScript (and C, C++, CUDA, ect), what this means is that the loop will execute the code between the end of the loop head and the next ;
.
Now consider this example.
for (let i = 0; i < 5; i++)
console.log(i)
In this case there is no ;
, so how does the loop know where to stop? Well, JavaScript will add a ;
before each new line automagically for you. So actually, this code is converted to the previous example before it is run.
Now we can screw with this. Consider this example
for (let i = 0; i < 5; i++) ;
console.log(i)
Since I used let
, we get a good error message that actually explains what’s going on here.
ReferenceError: i is not defined
at /home/runner/IncompatibleBitterNlp/index.js:2:17
at Script.runInContext (vm.js:131:20)
at Object.<anonymous> (/run_dir/interp.js:156:20)
at Module._compile (internal/modules/cjs/loader.js:1133:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
at Module.load (internal/modules/cjs/loader.js:977:32)
at Function.Module._load (internal/modules/cjs/loader.js:877:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
Because we did not use {} and we added that extra ;
, the console.log()
is now outside of the scope of the loop. But i
is only in scope for the loop since I used let
instead of var
. So when I hit that line, the code throws this error because I am trying to access the variable outside of the scope of the loop.
We can make the same changes to your loop to show what’s going on.
As is:
const arr = ["a", "b", "c", "d", "e"]; // You shouldn't name a variable after a keyword. I changed var -> arr
for (let i = 0; i < arr.length; i++);
console.log(arr[i]) ;
console.log(i);
shows this error
ReferenceError: i is not defined
at /home/runner/IncompatibleBitterNlp/index.js:3:18
at Script.runInContext (vm.js:131:20)
at Object.<anonymous> (/run_dir/interp.js:156:20)
at Module._compile (internal/modules/cjs/loader.js:1133:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
at Module.load (internal/modules/cjs/loader.js:977:32)
at Function.Module._load (internal/modules/cjs/loader.js:877:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
Removing the extra ;
:
const arr = ["a", "b", "c", "d", "e"];
for (let i = 0; i < arr.length; i++)
console.log(arr[i]);
console.log(i);
shows this error
a
b
c
d
e
ReferenceError: i is not defined
at /home/runner/IncompatibleBitterNlp/index.js:4:15
at Script.runInContext (vm.js:131:20)
at Object.<anonymous> (/run_dir/interp.js:156:20)
at Module._compile (internal/modules/cjs/loader.js:1133:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
at Module.load (internal/modules/cjs/loader.js:977:32)
at Function.Module._load (internal/modules/cjs/loader.js:877:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
Using braces:
const arr = ["a", "b", "c", "d", "e"];
for (let i = 0; i < arr.length; i++) {
console.log(arr[i]) ;
console.log(i);
}
shows this output
a
0
b
1
c
2
d
3
e
4