-2

var i = 0;
while (i < 10) {
  i++;
  if (i % 2 === 0) {
    continue;
  }
  alert(i);
}

why is this code working? cause if "i" be '4' then 4%2 = '2'! and this 2 is not same with 0 then why is it work??? as i know "===" mean exactly same. then why is it work??????

any help will be so appreciated thanks!

user13851719
  • 150
  • 8

2 Answers2

1

the % is for modulus and not for division.

in mathematics it defines the rest of a division, if a number is even divided by two the rest will equals 0 the modulus is commom used to find if a number is even or odd.

so it will work because 10 % 2 really equals 0

1

The % operator is not a division. It returns the remainder, for which 4 % 2 is 0.

You can see the documentation here. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Remainder

elPulpo
  • 41
  • 5