Computer >> Computer tutorials >  >> Programming >> Javascript

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


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!