Collect.js | when() Function Last Updated : 10 Jun, 2020 Comments Improve Suggest changes Like Article Like Report The when() function is used to callback if the first argument in the given proves to be true. In JavaScript, the array is first converted to a collection and then the function is applied to the collection.Syntax: data.when(conditional ,rule) Parameters: This function accept two parameters as mentioned above and described below: rule: This parameter holds the operation rule or the condition to be applied on the collection. conditional: This parameter holds the conditional value true or false. Return value : Return the modified collection list. Below examples illustrate the when() function in collect.js:Example 1: Here in this example, we take a collection and then using the when() function modify the collection. JavaScript // It is used to import collect.js library const collect = require('collect.js'); const collection = collect([0 , 1 , 2]); collection.when(true, items => items.push(3)); console.log(collection.all()); Output: [0 , 1 , 2 , 3] Example 2: Same as above example but applying a different operation to perform. JavaScript // It is used to import collect.js library const collect = require('collect.js'); const collection = collect([0 , 1 , 2]); collection.when(true, items => items.put('Jason')); console.log(collection.all()); Output: [ 0, 1, 2, Jason: undefined ] Reference: https://collect.js.org/api/when.html Comment More infoAdvertise with us Next Article Collect.js | when() Function akhilsharma870 Follow Improve Article Tags : JavaScript Web Technologies Collect.js Similar Reads Collect.js whereIn() Function The whereIn() function is used to filter the collection by a given key or value contained within the given array. In JavaScript, the array is first converted to a collection and then the function is applied to the collection. Syntax: data.whereIn('key') Parameters: This function accepts a single par 2 min read Collect.js where() Function The where() function is used to filter the collection by a given key or value contained within the given array. In JavaScript, the array is first converted to a collection and then the function is applied to the collection. Syntax: data.where('key') Parameters: This function accepts a single paramet 2 min read Collect.js whenEmpty() Function Collect.js is a fluent and convenient wrapper for working with arrays and objects. The whenEmpty() function executes the callback function when the collection is empty. Installation: Install the Collect.js module using the following command: npm install --save collect.jsSyntax:  collection.whenEmp 1 min read Collect.js make() Function Collect.js is a fluent and convenient wrapper for working with arrays and objects. The make() function creates a new collection instance. Installation: Install the Collect.js module using the following command: npm install --save collect.jsSyntax:  collection.make(user collection)Parameters: This f 2 min read Collect.js | push() Function The push() function is used to add a new value to the end of the collection.  In JavaScript, the array is first converted to a collection and then the function is applied to the collection. Syntax: data.push(value) Parameters: This function accepts a single parameter as mentioned above and describe 1 min read Like