JavaScript check if a Array or a string contains a value using check-more-types library

In this JavaScript tutorial we learn how to check whether a given array contains an item or a given string contains a substring using the contains() method of 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 if an array contains item in JavaScript

To check if a given array contains an item or not we can use the contains() method of check-more-types library. The method returns true if the given array contains the provided item value.

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

check-contains-array.js

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

console.log(check.contains(['black', 'blue', 'white', 123], 'blue'))
console.log(check.contains(['black', 'blue', 'white', 123], 123))
console.log(check.contains(['black', 'blue', 'white', 123], 'green'))

Run the check-contains-array.js file using the following command.

node check-contains-array.js
The output is:
true
true
false

How to check if a string contains substring in JavaScript

To check if a given string contains a substring or not we can use the contains() method of check-more-types library. The method returns true if the given string contains the provided substring value.

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

check-contains-string.js

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

console.log(check.contains('toriode.com', 'tori'))
console.log(check.contains('toriode.com', 'javascript'))

Run the check-contains-string.js file using the following command.

node check-contains-string.js
The output is:
true
false

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

check-contains-example.js

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

var sampleArray = ['javascript', 'python', 'node.js'];
var value1 = 'java';
var value2 = 'python';

if(check.contains(sampleArray, value1)) {
    console.log('the array contains value: ' + value1);
} else {
    console.log('The array does not contains value: ' + value1);
}

if(check.contains(sampleArray, value2)) {
    console.log('the array contains value: ' + value2);
} else {
    console.log('The array does not contains value: ' + value2);
}

if(check.contains(value1, 'py')) {
    console.log('The string ' + value1 + ' contains "py"' )
} else {
    console.log('The string ' + value1 + ' does not contains "py"' )
}

if(check.contains(value2, 'py')) {
    console.log('The string ' + value2 + ' contains "py"' )
} else {
    console.log('The string ' + value2 + ' does not contains "py"' )
}

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

node check-contains-example.js
The output is:
The array does not contains value: java
the array contains value: python
The string java does not contains "py"
The string python contains "py"

Happy Coding 😊