# (JavaScript programming Example for Beginners)

# Javascript Program to Check if a Number is Odd or Even

#### In this example, you will learn to write a JavaScript program to check if the number is odd or even.

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

The remainder operator `%`

gives the remainder when used with a number. Hence, when `%`

is used with **2**, the number is **even** if the remainder is zero. Otherwise, the number is **odd**.

## Example 1: Using if…else

```
// program to check if the number is even or odd
// take input from the user
let 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.

In the above program, `number % 2 == 0`

checks whether the number is **even**. If the remainder is **0**, the number is even.

In this case, **27 % 2** equals to **1**. Hence, the number is odd.

The above program can also be written using a ternary operator.

## Example 2: Using Ternary Operator

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

**Output**

Enter a number: 5 The number is odd.

## JS Example for Beginners: Javascript Program to Check if a Number is Odd or Even

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause.The information presented here could also be found in public knowledge domains.