# How to Test for Even or Odd Number in Javascript?

Even numbers are those numbers that are exactly divisible by 2.

The remainder operator % gives the remainder when used with a number. For example,

```
const number = 6;
const result = number % 4; // 2
```

Hence, when % is used with 2, the number is even if the remainder is zero. Otherwise, the number is odd.

## Using if...else

```
// program to check if the number is even or odd
// take input from the user
const number = prompt("Enter a number: ");
//check if the number is even
if(number % 2 == 0) {
console.log("The number is even.");
}
// if the number is odd
else {
console.log("The number is odd.");
}
```

### Output

Enter a number: 27

The number is odd.

The number is odd.

## Using Ternary Operator

```
// program to check if the number is even or odd
// take input from the user
const number = prompt("Enter a number: ");
// ternary operator
const result = (number % 2 == 0) ? "even" : "odd";
// display the result
console.log(`The number is ${result}.`);
```

### Output

Enter a number: 5

The number is odd.

The number is odd.