Open In App

Collect.js | push() Function

Last Updated : 12 Jun, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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 described below:

  • value: This parameter holds the value to be added.

Return Value: Returns a modified collection which is created by this function.

Below examples illustrate the push() function in collect.js
Example 1:

Javascript




// It is used to import collect.js library
const collect = require('collect.js');
  
const collection = collect(["Geeks", "for", "Geeks"]);
collection.push("CS");
console.log(collection.all());


Output: 

[ 'Geeks', 'for', 'Geeks', 'CS' ]

Example 2:

Javascript




// It is used to import collect.js library
const collect = require('collect.js');
  
const collection = collect([10 , 20 , 30]);
collection.push(40);
console.log(collection.all());


Output: 

[ 10, 20, 30, 40 ]

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


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads