# How can I check if a number is even or odd using JavaScript?

JavaScript, Math · Oct 6, 2023 The modulo operator (`%`) returns the remainder of a division operation. Given that, we can check if a number is even or odd by dividing it by `2` and checking the remainder. If the remainder is `0`, the number is even, otherwise it's odd.

```const isEven = num => num % 2 === 0;
const isOdd = num => num % 2 === 1;

isEven(3); // false
isOdd(3); // true```

The bitwise AND operator (`&`) returns `1` if both bits are `1`, otherwise it returns `0`. The binary representation of an even number always ends with `0`, while the binary representation of an odd number always ends with `1`. As such, applying the bitwise AND operator to a number and `1` will return `0` for even numbers and `1` for odd numbers. In order to convert this result to a boolean, we can use the `Boolean()` function.

```const isEven = num => !Boolean(num & 1);
const isOdd = num => Boolean(num & 1);

isEven(3); // false
isOdd(3); // true```

## More like this

• ### How can I remove trailing zeros from a number in JavaScript?

When formatting decimal values in JavaScript, trailing zeros can be undesired. Here's how to deal with them.

JavaScript, Math · May 8, 2022

• ### Luhn check

Implements the Luhn Algorithm, used to validate a variety of identification numbers.

JavaScript, Math · Jan 30, 2022

• ### What is the difference between prefix and postfix operators?

While both the prefix and postfix operators increment a value, the resulting value of the expression is very different.

JavaScript, Math · Oct 31, 2021