Tom Spatula
Tom Spatula

Reputation: 49

For loop (Java Script), why there is only single output and not as many as many times the loop iterates?

const count = () => {
  for (let i = 1; i <= 5; i++) {
    if ((2 + 2) == 4) {
      return (' Bla ')
    }
  }
};

console.log(count());

The output: 'Bla' And I was expecting: 'Bla' 'Bla' 'Bla' 'Bla' 'Bla'.

Upvotes: 1

Views: 47

Answers (1)

Mureinik
Mureinik

Reputation: 311798

In the first iteration, the function returns, so the loop does not continue. You could instead call console.log from within the loop directly:

const count = () => {
  for (let i = 1; i <= 5; i++) {
    if ((2 + 2) == 4) {
      console.log(' Bla ')
    }
  }
};

count();

Upvotes: 1

Related Questions