Node.js push() function12 Feb 2025 | 4 min read In this article, we will discuss the Node.js push() function with its syntax, parameters, and examples. What is the Node.js push() function?The push() method in Node.js is commonly associated with JavaScript arrays and is not a fundamental part of the Node.js runtime itself. An array can be modified by adding one or more elements to the end using the push() function. In order to add elements to the array, it accepts one or more arguments, each of which represents an element. In the order they are provided, the elements are appended to the end of the array. When the elements are added, the method returns the array's new length. Syntax:It has the following syntax: Parameters:
Return type:The function adds the element and then returns the array. Example 1:Let us take an example to illustrate the Node.js push() function. Output: The Modified Array elements are: [ 51, 35, 42, 16, 27, 31, 27 ] Explanation: In this example, the "PUSH" custom function is defined, and it adds a specified element to the end of an array while logging the modified array to the console. The function accepts two parameters: element, which is the element to be added, and "a", which is the array that needs to be modified. There is a declared array called "a" with initial values. After that, 27 is added to the array when the PUSH function is invoked with "a" as the array parameter and the number 27 as the element parameter. At last, the modified array is logged in the console, indicating every element, including the recently included 27. Example 2:Let us take another example to illustrate the Node.js push() function. Output: The Updated Array elements are: [ 'Lotus', 'Lilly', 'Rose', 'Jasmine' ] Explanation: In this example, we defines a "addFlower" custom function. It adds a configurable number of flower parameters to the end of an array. All flower parameters are gathered by the function using the rest parameter syntax and stored in an array called "flowers". After that, these flowers are added to the array "a" using the push() method and spread syntax. Following the array's update, the function logs the updated array to the console, containing both the original and the most recent flower elements. Finally, the method "addFlower" is called with the additional flowers ('Lilly, Rose, and Jasmine') to append them to the array after an array "a" is initialized with the value ['Lotus']. Example 3:Let us take another example to illustrate the Node.js push() function. Output: The Original Array elements are: [ 'Lotus', 'Lilly', 'Rose' ] The Updated Array elements are: [ 'Lotus', 'Lilly', 'Rose', 'Jasmine' ] Explanation: This code initializes an array called flowers with initial values that correspond to the three various kinds of flowers: rose, lotus, and Lilly. The original array contents are displayed on the console using the console.log() method. After that, we use the push() method to append the string "Jasmine" to the end of the flowers array. The modified array, with the addition of the new flower "Jasmine" is displayed on the console using another console.log() command. Overall, this code effectively demonstrates how to append elements to an array using the push() method in JavaScript. Next TopicNodejs-shift-function |
Error propagation and interception are key components of Node.js's ability to manage errors in asynchronous processes. The application may crash if function calls are not checked because they are one way that errors can propagate. Node.js uses techniques like error-first callbacks, promises with.catch(), and async/await...
4 min read
Request and Cheerio are two popular Node.js modules that are frequently used together for web data processing and web scraping. In this article, we will discuss Request and Cheerio in Node.js NPM with their key features, installations, syntaxes, and examples. What is Request in Node.js NPM? Request...
5 min read
Node Jimp is a powerful image-processing library for Node.js. Its API and ease of use have made it popular among developers. With the help of Jimp, developers are also able to carry out operations, such as resizing, cropping images, changing their colors, and much more,...
5 min read
Introduction: Modern software development relies heavily on Continuous Integration (CI) together with Continuous Deployment (CD) as its core operational practices. The automation system performs the work of code integration along with test execution and application deployment to maintain fast and trustworthy software delivery. The implementation of...
5 min read
Node.js has introduced the revolution in the server-side program by applying an event-driven non-blocking I/O model. Such a setup is suitable for managing multiple connections at once in this model. Fundamental of this model is the event loop , which manages asynchronous operation. Due to this,...
4 min read
The ability to arrange for a function or block of code to be executed at a later time; it can be scheduled to run once or repeatedly at regular intervals. This is known as task scheduling. The timers module in Node.js provides the underlying functionality on...
4 min read
Introduction The is fundamental. It is applied when we works with source maps at the development level. This function is often used to debug compiled and minimized JavaScript code, which helps debug complex applications written in JavaScript. A source map, in its basic form, is a...
3 min read
While both Netty and Node.js are tools used to develop network applications. In this article, we will discuss the difference between Netty and Node.js with their features, goals, and designs. Below is a thorough comparison of Node.js with Netty: What is Netty? The NIO client-server framework Netty makes...
5 min read
Introduction In Node.js, the console.error() function is a vital tool for developers to manage errors and debugging within their applications. As part of the global console object, it provides a straightforward way to output error messages to the standard error stream. This function is especially useful...
8 min read
In this article, we will discuss fs.Dirent.isBlockDevice() method in Node.js with its syntax, parameters, and example. What is the fs.Dirent.isBlockDevice() method in Node.js? In Node.js, the fs.Dirent.isBlockDevice() function is a part of the File System module's built-in API, which determines whether a dirent given describes a block device....
4 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India