JavaScript check bit value using check-more-types library

In this JavaScript tutorial we learn how to check a given number whether it is 0 or 1 value using the bit() 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 bit value in JavaScript

To check a given value whether it is bit we can use the bit() method of the check-more-types library. The method returns true if the provided value is 0 or 1.

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

check-bit.js

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

console.log(check.bit(1));
console.log(check.bit(0));
console.log(check.bit(2));
console.log(check.bit('test'));

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

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

More examples of how to use the check.bit() method

check-bit-example.js

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

var value1 = 1;
var value2 = 200;

if(check.bit(value1)) {
    console.log(value1 + ' is a bit value.');
} else {
    console.log(value1 + ' is not a bit value.');
}

if(check.bit(value2)) {
    console.log(value2 + ' is a bit value.');
} else {
    console.log(value2 + ' is not a bit value.');
}

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

node check-bit-example.js
The output is:
1 is a bit value.
200 is not a bit value.

Happy Coding 😊