GeeksforGeeks App
Open App
Browser
Continue

# 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 the number is even

Explanation: The calculation below shows how an even number is evaluated if & an operation with 1 is performed on it.

```2&1=0 then !(0) is true, so 2 is an even number
0 0 1 0
&
0 0 0 1
0 0 0 0
3&1=1 then !(1) is false, so 3 is an odd number
0 0 1 1
&
0 0 0 1
0 0 0 1```

Example:

## Javascript

 `function` `isEvenNumber(num) {``    ``if` `(!(num & 1)) {``        ``return` ``\${num} is an even number`;``    ``} ``else` `{``        ``return` ``\${num} is an odd number`;``    ``}``}``console.log(isEvenNumber(2));``console.log(isEvenNumber(3));`

Output:

```2 is an even number
3 is an odd number```

2. By multiplying and dividing a number by two: Even numbers do not lose decimal value so comparisons will be true. In the case of odd numbers, the decimal point value will be lost and comparisons will be false.

Example:

## Javascript

 `function` `isEvenNumber(num) {``if``(Math.floor(num / 2) * 2 === num) {``     ``return` ``\${num} is an even number`;``  ``} ``else` `{``     ``return` ``\${num} is an odd number`;``  ``}``}``console.log(isEvenNumber(2));``console.log(isEvenNumber(3));`

Output:

```2 is an even number
3 is an odd number```

3. By using Regular Expression (JavaScript Regex): In this method, we check the last digit number. If the last digit number falls between (0,2,4,6,8) then the number is an even number otherwise a number is an odd number.

Example:

## Javascript

 `function` `isEvenNumber(num) {``if``(/^\d*[02468]\$/.test(num)) {``     ``return` ``\${num} is an even number`;``  ``} ``else` `{``     ``return` ``\${num} is an odd number`;``  ``}``}``console.log(isEvenNumber(2));``console.log(isEvenNumber(3));`

Output:

```2 is an even number
3 is an odd number```

4. By using a predefined method of number class: We use the isInteger() method of the number class to determine whether the number belongs to the integer or not. The function returns false if the number is not an integer, and returns true if the number is an integer.

```2/2 =0  where 0 is an integer
3/2=1.5 where 1.5 is an integer```

Example:

## Javascript

 `function` `isEvenNumber(num) {``if``(Number.isInteger(num / 2)) {``     ``return` ``\${num} is an even number`;``  ``} ``else` `{``     ``return` ``\${num} is an odd number`;``  ``}``}``console.log(isEvenNumber(2));``console.log(isEvenNumber(3));`

Output:

```2 is an even number
3 is an odd number```

5. By using conditional Loop: We subtract a number from 2 until the number is less than 2. Then we are going to see if the remaining number is 1 or 0. if the answer is 1 then the number is an odd number and else the number is an even number.

Example:

## Javascript

 `function` `isEvenNumber(num) {` `  ``// Math.abs() is for negative number``  ``num = Math.abs(num);``  ``while` `(num >= 2) {``     ``num = num - 2;``  ``}``  ``if` `(num === 1) {``     ``return` ``\${num} is an odd number`;``  ``} ``else` `{``     ``return` ``\${num} is an even number`;``  ``}``}``console.log(isEvenNumber(2));``console.log(isEvenNumber(3));`

Output:

```2 is an even number
3 is an odd number```

My Personal Notes arrow_drop_up
Related Tutorials