0

I'm running the below code to print out numbers from 0 to 6. It works, but I can't figure out the reason why the code prints out the number 7. Why is the code still running even though number variable is now 7:

let number = 0;
while (number <= 6) {  
console.log(number);  
number = number + 1
}

I expected the code to only print up to 6 and then stop as any number above 6 would make the expression false

  • 4
    the code prints from 0 to 6 – Chris G Feb 06 '23 at 12:21
  • Are you running this in the console? You'd get the result of the last line but *this is not printed with `console.log()`*. It's just the REPL behaviour of the console. – VLAZ Feb 06 '23 at 12:21
  • Yes I am, I wasn't aware of this. Now I feel like an idiot. Thanks so much – trojan_pig Feb 06 '23 at 12:23
  • 1
    :-) No need to feel like an idiot, but just take away the fact that the browser console is a tricky environment. Best to test code in the actual way you'll use it, rather than in the console. – T.J. Crowder Feb 06 '23 at 12:24

0 Answers0