Open In App

Collect.js unlessNotEmpty() Function

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Collect.js is a fluent and convenient wrapper for working with arrays and objects. The unlessNotEmpty() function executes the callback function when the collection is empty. 

Installation: Install the Collect.js module using the following command:

npm install --save collect.js

Syntax:  

collection.unlessNotEmpty(callback)

Parameters: This function takes only one parameter i.e. the callback function which executes if the collection is empty.

Return Value: This function returns the new collection object.

Example 1: Filename-index.js 

JavaScript
// Requiring module
const collect = require('collect.js')

// User defined collection
var myCollection = []

// Creating collection object
const collection = collect(myCollection);

// Callback execution since collection object is empty
collection.unlessNotEmpty(item => item.push(1));

// Printing collection
console.log(collection.all());

 
 

Run the index.js file using the following command:


 

node index.js

Output:


 

[ 1 ]

Example 2: Filename-index.js 


 

JavaScript
// Requiring module
const collect = require('collect.js')

// User defined collection
var myCollection = [1, 2, 3]

// Creating collection object
const collection = collect(myCollection);

// No callback execution since 
// collection is not empty
collection.unlessNotEmpty((item) => {
  item.push(4)
  item.push(5)
});

// Printing collection
console.log(collection.all());

 
 

Run the index.js file using the following command:


 

node index.js

Output:


 

[ 1, 2, 3]


 

Reference: https://collect.js.org/api/unlessnotempty


 


Similar Reads