0

If I omit the 'let' or 'var' from a javascript for-loop, it still works. So instead of declaring "let i = 0" in the loop and just typing "i = 0" it still seems to have the same scope and works the same. Why is that?

How can the "i++" part work without knowing what type it is and without knowing its start value?

This code still executes properly:


<p id="demo"></p>
<p id="i"></p>

<script>
const cars = ["BMW", "Volvo", "Saab", "Ford", "Fiat", "Audi"];

let text = "";
let iter = "";
for (i = 0; i < cars.length; i++) {
  text += cars[i] + "<br>";
  iter = i;
}

document.getElementById("demo").innerHTML = text;
document.getElementById("i").innerHTML = iter;
</script>```
Jirosworld
  • 27
  • 6
  • 1
    knowing its start value? wasn't that the =0? – Abel Oct 14 '21 at 11:48
  • 1
    `var i = 0` declares a variable `i`. If you omit `var` you just have an implicit global. Same with `let` and `const` – VLAZ Oct 14 '21 at 11:49
  • 1
    If you start the script with `'use strict';` you will see that it declares an implicit global. I would really encourage you to always use strict mode when learning JS. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode – max Oct 14 '21 at 11:51
  • I had no idea variables still work without using var/let/const; this explains a lot and seems a bad idea :-) – Jirosworld Oct 14 '21 at 12:50
  • @jolarti yes, [it's horrible](http://blog.niftysnippets.org/2008/03/horror-of-implicit-globals.html) – VLAZ Oct 18 '21 at 08:12

1 Answers1

3

In JavaScript, if you omit the "var", "let" or "const" keyword during a variable initialization, it will be initialized as a GLOBAL variable.

Your "i" variable will be visible outside the for loop. The loop works, of course since it IS initialized properly, but be aware of all the drawbacks of declaring global variables.

Just to have a better clue, you can try to manipulate this "i" variable outside the for loop, or just using a

console.log(i)

In other words declaring a variable (let's call it "index") without using "var", "let" or "const" it will be equivalent of declaring it as

window.index = 0;
//100% equivalent to
index = 0;

EDIT: an exception I didn't mention is the case you use the "strict mode" https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode

In this case it will throw an exception as you would initially expect. To declare variables in strict mode you have to use "let", "var" or "const" keywords.

If you use the strict mode, the only way to declare global variables is through window Object

window.yourNewGlobalVariable = 'whatever' //works in strict mode
yourNewGlobalVariable = 'whatever' // doesn't work in strict mode
Picchio
  • 425
  • 1
  • 7
  • Thank you; I wish I had enough rights to mark this as very helpful. I had no idea variables still work without using var/let/const; and because of a lack of that basic knowledge I was not able to associate my question with any similar ones. – Jirosworld Oct 14 '21 at 12:57