1

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'.

2
  • Put console log inside the loop or else return a array of values and print them. Commented Mar 20, 2020 at 12:16
  • for i=1, the codition gets true and return you the value Commented Mar 20, 2020 at 12:19

1 Answer 1

1

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();

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.