JavaScript check if a number is positive or not using check-more-types library

In this JavaScript tutorial we learn how to check a number value to determine if it is positive or not using the positive() method of the check-more-types module.

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 positive or not in JavaScript

To check whether a given number is a positive number or not we can use the positive() or not.positive() method of check-more-types library. The positive() method returns true ì the argument value is positive number. The not.positive() method returns true if the provided argument is a negative number.

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

check-positive.js

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

console.log(check.positive(10))
console.log(check.positive(-10))

console.log(check.not.positive(10));
console.log(check.not.positive(-10));

Run the check-positive.js file using the following command.

node check-positive.js
The output is:
true
false
false
true

More examples of how to use the check.positive() and check.not.positive() method

check-positive-example.js

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

var number1 = 7;
var number2 = -7;

if(check.positive(number1)) {
    console.log(number1 + ' is a positive number.');
}

if(check.positive(number2)) {
    console.log(number2 + ' is a positive number.');
}

if(check.not.positive(number1)) {
    console.log(number1 + ' is not a positive number.');
}

if(check.not.positive(number2)) {
    console.log(number2 + ' is not a positive number.');
}

Run the check-positive-example.js file using the following command.

node check-positive-example.js
The output is:
7 is a positive number.
-7 is not a positive number.

Happy Coding 😊