How to determine if a number is odd or even in JavaScript?

JavascriptWeb DevelopmentFront End Technology

Use the modulus % operator to determine if a number if odd or even in JavaScript.

Example

You can try to run the following code to check if a number is odd or even −

Live Demo

<!DOCTYPE html>
<html>
<body>
<script>
   var num = 5;
   document.write("Number = "+num+"<br>");
   if(num % 2 == 0) {
      document.write('Number is even!');
   } else {
      document.write('Number is odd!');
   }
</script>
</body>
</html>

Output

Number = 5
Number is odd!
raja
Published on 03-Feb-2018 17:11:28
Advertisements