JavaScript check if a number is odd or even using check-more-types library

In this JavaScript tutorial we learn how to check whether a number is even or odd using the check-more-types library.

Create the sample project

Firstly we create an empty project with the following command.

mkdir sample-project
cd sample-project
npm init -y

And install the check-more-types module using the following command.

npm install check-more-types --save

How to check a number is even or odd in JavaScript

In this section we show you how to use the check-more-types library odd() and even() method to check if a given number is an even or odd number.

In the sample-project directory create a new file named check-odd-even.js as below.

check-odd-even.js

var check = require('check-more-types');

console.log(check.odd(1));
console.log(check.odd(2));

console.log(check.even(3));
console.log(check.even(4));

Run the check-odd-even.js file using the following command.

node check-odd-even.js
The output is:
true
false
false
true

More examples of how to use the check.even() and check.odd() method

check-odd-even-example.js

var check = require('check-more-types');

var number = 7;

if(check.odd(number)) {
    console.log(number + ' is an odd number.')
} else {
    console.log(number + ' is not an odd number.')
}

if(check.even(number)) {
    console.log(number + ' is an even number.')
} else {
    console.log(number + ' is not an even number.')
}

Run the check-odd-even-example.js file using the following command.

node check-odd-even-example.js
The output is:
7 is an odd number.
7 is not an even number.

Happy Coding 😊