JavaScript check if a number is a valid port using check-more-types library

In this JavaScript tutorial we learn how to check if a given number is a valid port number or not using the check-more-types library. Via different JavaScript examples we learn how to validate port, system port or user port number.

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 validate port in JavaScript

To check if a given number is a valid port or not we can use the port() method of check-more-types library. The method returns true if the input value is a positive number less or equal to 65535.

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

check-port.js

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

console.log(check.port(8080));
console.log(check.port(67000));

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

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

How to validate system port in JavaScript

To check if a given number is a valid system port or not we can use the systemPort() method of check-more-types library. The method returns true if the input number is between 0 and 1024.

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

check-system-port.js

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

console.log(check.systemPort(1024));
console.log(check.systemPort(8080));

Run the check-system-port.js file using the following command.

node check-system-port.js
The output is:
true
false

How to validate user port in JavaScript

To check if a given number is a valid user port or not we can use the userPort() method of check-more-types library. The method returns true if the input number is larger than 1024 and less than or equal 65535.

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

check-user-port.js

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

console.log(check.userPort(8080));
console.log(check.userPort(1024));

Run the check-user-port.js file using the following command.

node check-user-port.js
The output is:
true
false

More examples of how to use the check.port(), check.systemPort() and check.userPort() method

check-port-examples.js

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

var portNumber = 8080;

if(check.port(portNumber)) {
    console.log(portNumber + ' is a valid port number.');
} else {
    console.log(portNumber + ' is an invalid port number.');
}

if(check.systemPort(portNumber)) {
    console.log(portNumber + ' is a system port number.');
} else {
    console.log(portNumber + ' is not a system port number.');
}

if(check.userPort(portNumber)) {
    console.log(portNumber + ' is a user port number.');
} else {
    console.log(portNumber + ' is not a user port number.');
}

Run the check-port-examples.js file using the following command.

node check-port-examples.js
The output is:
8080 is a valid port number.
8080 is not a system port number.
8080 is a user port number.

Happy Coding 😊