
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
JavaScript: How to check if a number is even without using the modulo operator?
Often we need to check whether a number is odd or even and return the same to the user. In this article also we will be checking whether a number is even or not but without using the modulo operator. It’s pretty easy to check whether a number is even by using the modulo operator. But we can also check a number with other possible methods.
Below we are going to see the other methods for checking a number and its properties.
Approach #1: Using the for() loop
In this method, we are going to use the for() loop to check whether a number is even or not. The idea is to take a Boolean flag variable as true and check it up to n times. If the flag gets its value back it means the value was even else not.
Example 1
In the below example, we are going to check whether a number is even or not by using the for() loop approach.
# index.html
<!DOCTYPE html> <html> <head> <title>Checking If a Number is Even</title> </head> <body> <h1 style="color: green"> Welcome To Tutorials Point </h1> <script> // Returns true if n is even, function isEven(n) { let isEven = true; for(let i = 1; i <= n; i++) isEven = !isEven; if (isEven) console.log(n + " is an Even number."); else console.log(n + " is Odd."); } // Driver Code isEven(101); isEven(158); </script> </body> </html>
Output
The above program will produce the result in the console similar to the below screenshot −
Approach #2: Using multiplication and division
In this method, we are going to divide a number by 2 and then multiply the result by 2. If the result is the same as that of the original number, it is an even number.
Example 2
In the below example, we are going to check whether a number is even or not by using multiplication and division.
# index.html
<!DOCTYPE html> <html> <head> <title>Checking If a Number is Even</title> </head> <body> <h1 style="color: green;"> Welcome To Tutorials Point </h1> <script> // Returns true if n is even, function isEven(n) { // Return true if n/2 does not result // in a float value. if(parseInt(n / 2, 10) * 2 == n) { console.log(n + " is an Even number."); } else { console.log(n + " is Odd."); } } // Driver Code isEven(101); isEven(158); </script> </body> </html>
Output
The above program will produce the result in the console similar to the below screenshot −
Approach #3: Using the bitwise operator
In this method, we are going to use the bitwise AND operator to check the number.
Example #3
# index.html
<!DOCTYPE html> <html> <head> <title>Checking If a Number is Even</title> </head> <body> <h1 style="color: green;"> Welcome To Tutorials Point </h1> <script> // Returns true if n is even, function isEven(n) { // n&1 is 1, then odd, else even if (!(n & 1)) { console.log(n + " is an Even number."); } else { console.log(n + " is Odd."); } } // Driver Code isEven(101); isEven(158); </script> </body> </html>
Output
- Related Articles
- Program to find remainder without using modulo or % operator in C++
- How to Check if a Number is Odd or Even using Python?
- How to check if a number evaluates to Infinity using JavaScript?
- How to determine if a number is odd or even in JavaScript?
- What is modulo % operator in Python?
- Program to check if a number is Positive, Negative, Odd, Even, Zero?
- Check if a number is multiple of 5 without using / and % operators in C++
- JavaScript: How to check if a number is NaN or finite?
- Haskell Program to check the given number is an EVEN number using library function
- C# Program to check if a number is Positive, Negative, Odd, Even, Zero
- PHP program to check if the total number of divisors of a number is even or odd
- Returning the nth even number using JavaScript
- How to check if a string is html or not using JavaScript?
- How to check if an object is empty using JavaScript?
- How To Check If The Number Is A Prime Number In Excel?
