0

Why can't I declare a variable inside a for loop below?

Wrong:

  for(let i = 1; i <= num; i++){
    let factorials = [];
    if(num % i === 0){
      factorials.push(i);
    }
  }
// ReferenceError: factorials is not defined

Correct:

  let factorials = [];
  for(let i = 1; i <= num; i++){
    if(num % i === 0){
      factorials.push(i);
    }
  }
sflow
  • 655
  • 2
  • 8
  • 18
  • Because scope of `factorials` is not outside of the loop – Kunal Mukherjee Jan 17 '19 at 10:07
  • `S2015 introduced two important new JavaScript keywords: let and const. These two keywords provide Block Scope variables (and constants) in JavaScript.`, let only set a variable in the block context, so if you try to call it outside the block (the for loop in your case) you won't be able to do so – Art3mix Jan 17 '19 at 10:07
  • 2
    because after the loop finishes, the variable declared with `let` are erased and you can't use it on the rest of your code – Vencovsky Jan 17 '19 at 10:07
  • 1
    Possible duplicate of [What's the difference between using "let" and "var" to declare a variable in JavaScript?](https://stackoverflow.com/questions/762011/whats-the-difference-between-using-let-and-var-to-declare-a-variable-in-jav) – adiga Jan 17 '19 at 10:08
  • You *can* declare a variable inside a for loop. Just can't use outside of it because `let` is scoped to the nearest enclosing block `{ }`. Go through the answers posted in the duplicate. – adiga Jan 17 '19 at 10:10
  • FYI, in your wrong snippet, the error is `num` is not defined. And if `num` is defined, then there is no error. Just your output is not as expected – maazadeeb Jan 17 '19 at 10:10

4 Answers4

0

If you define a variable inside of { }. It means that the variable is only alive inside of the { }. It's called a scope.

You have a scope using if/while/for/function...

In the following example, the factorials variable is alive only inside of one loop iteration of the for. Which means that you are recreating a factorials variable every time you loop.

  for(let i = 1; i <= num; i++){
    const factorials = [];

    if(num % i === 0){
      factorials.push(i);
    }
  }

If you want to make it work, you have to create your variable outside of the for so it will not be recreate after every iteration, and keep it's values.

  const factorials = [];

  for(let i = 1; i <= num; i++){
    if(num % i === 0){
      factorials.push(i);
    }
  }
Orelsanpls
  • 22,456
  • 6
  • 42
  • 69
0

In the first code snippet, you are declaring factorials inside the for loop block, hence it will not be visible outside the for loop block.

It is working in the 2nd snippet because you declared it outside of block.

ScrapCode
  • 2,109
  • 5
  • 24
  • 44
0

Why can't I declare a variable inside a for loop below?

No, you can.

Here is the proof.

let num = 5;

for (let i = 1; i <= num; i++) {
  let factorials = [];
  if (num % i === 0) {
    factorials.push(i);
  }
}

let factorials = [];
for (let i = 1; i <= num; i++) {
  if (num % i === 0) {
    factorials.push(i);
  }
}

console.log("No Error.")
holydragon
  • 6,158
  • 6
  • 39
  • 62
0

Yes, you can, but if you declare the factorials-array in the loop it's only available in that closure. So consoling it out of the loop won't work.

Here's a very informative article about closures: https://javascript.info/closure

The whole javascript.info-site is very interesting, I am studying it for now to strengthen my javascript basics and fill not known informations-holes, although I am into web developing since years. But it never hurts to get more knowledge, even if its the basics.

Also I would advice you to use "use strict;" from beginning when developing, it helps to keep your scripts more clean, disallowing sources of errors.

JestaBlunt
  • 143
  • 2
  • 11