JavaScript Program to Check if a Number is Odd or Even
Last Updated :
28 Apr, 2025
We are going to learn how to check whether the number is even or Odd using JavaScript. In the number system, any natural number that can be expressed in the form of (2n + 1) is called an odd number, and if the number can be expressed in the form of 2n is called an even number.
- Even = {2k : k ∈ Z}(where k is an integer)
- Odd = {2k + 1 : k ∈ Z}(where k is an integer. )
Approaches to Check if a Number is Odd or Even
Here are the different approaches we can use in JavaScript to determine whether a number is odd or even:
1. Using the modulo Operator
In this method, we use the Modulo Operator (%) to check if a number is even or odd. The modulo operator gives the remainder when one number is divided by another. To check if a number is even or odd:
- If the result of N%2 is 0, the number is even.
- If the result is 1, the number is odd.
Syntax
remainder = var1 % var2
Now let’s understand this with the help of an example:
JavaScript
function isEven(n) {
return (n % 2 == 0);
}
let n = 101;
isEven(n) ? console.log("Even") : console.log("Odd");
2. Using Bitwise & Operator
A faster way to check if a number is even or odd is by using the Bitwise AND Operator (&). This checks the last bit of a number.
- If the last bit is 1, the number is odd.
- If the last bit is 0, the number is even.
Syntax
a & b
Now let’s understand this with the help of example:
JavaScript
function checkOddOrEven(n) {
if (n & 1 == 1) {
return "Number is odd";
}
return "Number is even";
}
console.log(checkOddOrEven(12));
console.log(checkOddOrEven(121));
OutputNumber is even
Number is odd
3. Using Bitwise OR Operator (|)
The Bitwise OR Operator (|) compares two numbers bit by bit. It returns 1 for each bit position where at least one of the bits is 1. If both bits are 0, it returns 0. In simple words, the OR operator checks two bits and returns 1 if either of the bits is 1. If both bits are 0, it returns 0.
Syntax
a | b
Now let’s understand this with the help of example
JavaScript
function checkOddOrEven(number) {
return (number | 1) === number ? 'Odd' : 'Even';
}
console.log(checkOddOrEven(14));
console.log(checkOddOrEven(17));
4. Using if…else Statement
The if…else statement is a fundamental structure in JavaScript. It allows you to execute different blocks of code depending on whether a condition is true or false. This can be used to check if a number is odd or even.
Syntax
if (condition) {
// Block of code if condition is true
} else {
// Block of code if condition is false
}
JavaScript
let num = 4;
if (num % 2 === 0) {
console.log("The number is even.");
} else {
console.log("The number is odd.");
}
OutputThe number is even.
5. Using the Ternary Operator
The Ternary Operator is a shorthand for the if…else statement. It takes three operands: a condition, a result for true, and a result for false. This can be used to check whether a number is even or odd in one line.
Syntax
condition ? expr1 : expr2;
JavaScript
let num = 8;
let result = (num % 2 === 0) ? "The number is even." : "The number is odd.";
console.log(result);
OutputThe number is even.
Conclusion
In JavaScript, there are multiple ways to check if a number is even or odd, including using the Modulo Operator (%), Bitwise AND Operator (&), and Bitwise OR Operator (|). The modulo operator is the simplest and most commonly used, while the bitwise operators offer more efficient solutions, particularly for performance-sensitive applications.
Similar Reads
JavaScript Program to Check if a Number is Float or Integer
In this article, we will see how to check whether a number is a float or an integer in JavaScript. A float is a number with a decimal point, while an integer is a whole number or a natural number without having a decimal point. Table of ContentUsing the Number.isInteger() MethodUsing the Modulus Ope
2 min read
PHP check if a number is Even or Odd
Checking if a number is even or odd involves determining whether the number is divisible by 2. An even number has no remainder when divided by 2, while an odd number has a remainder of 1. This can be done using various methods like modulo or bit manipulation. Examples : Input : 42 Output : Even Expl
3 min read
JavaScript Program to Check if a Number is Odd or Not using Bit Manipulation
In JavaScript, bit manipulation is the process of manipulating individual bits in a number for various operations. In this article, we will explore two approaches using bit manipulation to Check whether a number is odd or not. Below are the approaches to Check if a number is odd or not using bit man
2 min read
JavaScript program to print even numbers in an array
Given an array of numbers and the task is to write a JavaScript program to print all even numbers in that array. We will use the following methods to find even numbers in an array: Table of Content Method 1: Using for Loop Method 2: Using while Loop Method 3: Using forEach LoopMethod 4: Using filter
5 min read
How to check a number is even without using modulo operator in JavaScript ?
In this article, we learn how to check if a number is even without using the % or modulo operator. The methods are mentioned below. 1. By using the bitwise '&' operator: When & operation is done on a number with 1 it will return 0 if the number is zero. We will use this method to check if th
3 min read
Javascript Program To Check Whether The Length Of Given Linked List Is Even Or Odd
Given a linked list, the task is to make a function which checks whether the length of the linked list is even or odd. Examples: Input : 1->2->3->4->NULLOutput : EvenInput : 1->2->3->4->5->NULLOutput : OddMethod 1: Count the codes linearly Traverse the entire Linked List a
3 min read
Javascript Program For Segregating Even And Odd Nodes In A Linked List
Given a Linked List of integers, write a function to modify the linked list such that all even numbers appear before all the odd numbers in the modified linked list. Also, keep the order of even and odd numbers same. Examples: Input: 17->15->8->12->10->5->4->1->7->6->NU
6 min read
Print all Even Numbers in a Range in JavaScript Array
We have to find all even numbers within a given range. To solve this question we are given the range(start, end) in which we have to find the answer. There are several ways to print all the even numbers in a range in an array using JavaScript which are as follows: Table of Content Using for Loop in
3 min read
Sum of Squares of Even Numbers in an Array using JavaScript
JavaScript program allows us to compute the sum of squares of even numbers within a given array. The task involves iterating through the array, identifying even numbers, squaring them, and summing up the squares. There are several approaches to find the sum of the square of even numbers in an array
2 min read
Is Zero Even or Odd Number?
Zero is an even number because it is an integral multiple of 2. When 0 is divided by 2, the resulting quotient is also 0, which is an integer. This makes it an even number. Let's understand why zero is an even number in some detail. Table of Content Is Zero Even or Odd NumberWhat are Even Numbers?Wh
6 min read