Thursday, November 20, 2025
HomeLanguagesJavascriptCollect.js contains() Method

Collect.js contains() Method

The contains() method is used to determines whether the collection contains a given item or not. If it contains the item then it returns true otherwise false. The JavaScript array is first transformed into a collection and then the function is applied to the collection.

Syntax:  

collect(array).contains(item)

Parameters: This method accepts single parameter i.e. converted into the collection and then contains() function take an item to be searched.

Return Value: Returns a boolean value.

Below examples illustrate the contains() method in Collect.js:

Example 1: Here collect = require(‘collect.js’) is used to import the collect.js library into the file.

Javascript




const collect = require('collect.js'); 
     
let arr = [1, 2, 3]; 
     
// Convert array into collection 
const collection = collect(arr); 
 
// item to searched
let item = 3;
 
let newObject = collection.contains(item);
     
console.log("Result : ", newObject);


Output:

Result :  true

Example 2:

Javascript




const collect = require('collect.js'); 
     
let arr = [1, 2, 3] 
     
// Convert array into collection 
const collection = collect(arr); 
 
// concat the array
let concatarr = collection.concat(['a', 'b', 'c']);
 
// concat the object
concatarr = concatarr.concat({ first : "neveropen",
        second : "Collect.js"});
 
// item to searched
let item = "GeeksforGeek";
 
let newObject = collection.contains(item);
     
console.log("Result : ", newObject);


Output:

Result : false

Reference: https://collect.js.org/api/contains.html

RELATED ARTICLES

Most Popular

Dominic
32404 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6776 POSTS0 COMMENTS
Nicole Veronica
11925 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11994 POSTS0 COMMENTS
Shaida Kate Naidoo
6905 POSTS0 COMMENTS
Ted Musemwa
7160 POSTS0 COMMENTS
Thapelo Manthata
6861 POSTS0 COMMENTS
Umr Jansen
6846 POSTS0 COMMENTS