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


In this tutorial, let’s see how to determine whether a number is odd or even using JavaScript. The first step is understanding the logic. What are even numbers? Even numbers will be exactly divisible by 2. The remainder will be zero. What are odd numbers? Odd numbers will carry a remainder when divided by 2.

Using the if-else Condition and Modulo Operator

"if" will check a condition, and the if block code will be executed if the condition is true. If the condition is false, else block code will be executed.

The modulo operator in JavaScript returns the remainder.

Syntax

Users can follow the below syntax to use the if else statement and modulus operator.

if(number % 2==0){
   
   //The number is even
}
else {
   
   //The number is odd
}

Algorithm

  • STEP 1 − Read the number to variable n.

  • STEP 2 − Divide n by 2 and save the remainder in a variable named r.

  • STEP 3

    If r==0, print "even"
    If r!=0, print "odd"
    

Example

In this example, we have assigned 156 to a variable. Next, we apply the syntax given above. Here, if block checks for the remainder and it is 0 here. Hence the if block gets executed and we get the output as "156 is Even".

<html> <body> <h2><i>if else</i> and <i>modulus operator</i> to find odd or even</h2> <div id="number"></div> <script> let numoutput=document.getElementById("number"); var nmbr = 156; if(nmbr % 2 == 0) numoutput.innerHTML = nmbr + " is Even"; else numoutput.innerHTML = nmbr + " is Odd"; </script> </body> </html>

Using the Ternary and Modulo Operators

A ternary operator is an operator with three operands. The condition, a value if the condition is true, and another value if the condition is false are the three operands of the expression.

If the condition is true, the value between "?" and ":" is executed. Similarly, if the condition returns false, the value that follows ":" is performed.

Syntax

Users can follow the below syntax to use the ternary operator.

var result = condition? "true block": "false block";

Example

In this example, 131 is assigned to a variable. Next, we check for its remainder using the syntax above. Here the remainder is 1. So, the false block of the ternary operation gets executed. Hence, we get the output "number is odd".

<html> <body> <h2>Using <i>Ternary Operator</i> and <i>modulus operator</i></h2> <div id="number"></div> <script> let output=document.getElementById("number"); var nbr = 131; (nbr % 2 == 0) ?output.innerHTML = nbr +" is even" : output.innerHTML= nbr+ " number is odd"; </script> </body> </html>

Using Bitwise XOR Operator

When a number is bitwise XORed by 1, the number gets incremented by 1, if it is even and gets decremented by 1, if it is odd.

Consider 11 XOR 1
1011
0001
-----
1010 is 10, and it's a decrement; hence 11 is an odd number.

Users can follow the below syntax to use the bitwise xor operator.

Syntax

if ((n ^ 1) == (n + 1))//bitwise xor logic
   //even
else
   //odd

Example

In this example, we take the number 100. Using the ternary operation, we pass it to an odd-even check function where the bitwise XOR logic is checked and a boolean value is returned. If it is true, we display that the number is "even" or else the number is "odd".

<html> <body> <h2>Using <i>bitwise xor operator</i></h2> <div id="number"></div> <script> function isOddEven(n) { if ((n ^ 1) == (n + 1))//even return true; else //odd return false; } let output=document.getElementById("number"); var num = 100; isOddEven(num) ? output.innerHTML = num + " Even" : output.innerHTML = num + " Odd" ; </script> </body> </html>

Using Bitwise AND Operator

When a number undergoes AND with 1 and if the last bit of the result is 1, it is an odd number. If the last bit is zero, it is an even number.

Take the binary value of numbers 10 and 1. Then AND both.
0000 1010
0000 0001
--------------
0000 0000
Here the output bits are zero and hence 10 is an even number.

Syntax

Users can follow the below syntax to use bitwise and operator.

if(number & 1)//bitwise and logic
   //odd
else
   //even

Example

In this example, first, we pass 93 to the check function. The function performs bitwise AND operation, as we have seen above. If 93 & 1 returns 1, it's considered odd and else considered even. This boolean value is then used to display the output "93 odd" using the document. write method.

<html> <body> <h2>Using <i>bitwise and operator</i></h2> <div id="number"></div> <script> let output=document.getElementById("number"); function check(number) { var isOdd = number & 1; var isEven= !(number & 1); if(isOdd) { output.innerHTML= "
"
+ number + " odd"; } else output.innerHTML = "
"
+ number + " even"; } check(46); </script> </body> </html>

Using Bitwise OR Operator

When a number undergoes bitwise OR with 1 and if the result is the same number, it is an odd number. If the number gets incremented by 1, it is an even number.

Take 1011 OR 1
1011
0001
------
1011
Here the output bits are equal to 11 hence 11 is an odd number.

Syntax

Users can follow the below syntax to use bitwise or operator.

if ((n | 1) > n)//bitwise or logic
   //even
else
   //odd

Example

In this example, we take the number 100 and pass it to the check function. Here the number undergoes bitwise OR logic as explained above. If the result is greater than the number, it is even. Else it is odd. Here bitwise OR result of 100 is greater than 100 and hence 100 is an even number. From the boolean value returned by the check function, we display the output using the document. write method.

<html> <body> <h2>Using <i>bitwise or operator</i></h2> <div id="number"></div> <script> function checkOddEven(n) { if ((n | 1) > n)//even return true; else //odd return false; } let output=document.getElementById("number"); var n = 100; output.innerHTML = checkOddEven(n) == true ? n + " is Even": n + " is Odd"; </script> </body> </html>

Updated on: 02-Sep-2023

38K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements