Adding Elements to a List in JavaScript29 Mar 2025 | 3 min read In JavaScript, adding elements to an array or list can be done in many ways. Every method has its unique application and easy accessibility. Every method is useful and choosing the best one makes the problem optimized. Let us see the methods that are used to add elements to the array in JavaScript in 10 different ways.
Realizing these strategies empowers you to manage different situations handily and ensures that you can work with arrays in the best way for your prerequisites. Here is a more critical glance at a couple of additional things to contemplate while applying every strategy. 1. push()The push technique changes the length of an array by appending at least one element to its end. Code Output: [ 'apple', 'banana', 'orange', 'grape' ] 2. unshift()The unshift function changes the length of an array by appending at least one element to its beginning. Code Output: [ 'mango', 'pineapple', 'apple', 'banana' ] 3. concat()The concat function joins at least two arrays together. This technique returns another array as opposed to changing the all-around existing arrays. Code Output: [ 'apple', 'banana', 'cherry', 'strawberry' ] 4. Spread OperatorA genuinely concise method for adding elements to an array is through the spread operator. Code Output: [ 'apple', 'banana', 'cherry', 'strawberry' ] 5. splice()The splice function adjusts an array's items by embedding new elements in lieu of or erasing old ones. Code Output: [ 'apple', 'cherry', 'strawberry', 'banana' ] 6. slice()Although it does not add elements straightforwardly, the slice() method can be utilized related to different procedures to expand the size of an array. Code Output: [ 'apple', 'cherry', 'strawberry', 'banana' ] 7. Array.from()Utilizing an array-like or iterable object, Array.from() creates another example of the array. You can add items in an imaginative manner utilizing this system. Code Output: [ 'apple', 'banana', 'cherry', 'strawberry' ] 8. apply()Several elements can be pushed into an array using the apply() method. Code Output: [ 'apple', 'banana', 'cherry', 'strawberry' ] 9. reduce()Each element of the array is exposed to a reducer capability utilizing the reduce() method, which creates a solitary result value. Remembering more items for an array can be adjusted. Code Output: [ 'apple', 'banana', 'cherry', 'strawberry' ] 10. map()Map() can be utilized to create another array with additional elements, even though changing array elements is essentially utilized. Code Output: [ 'apple', 'banana', 'cherry', 'strawberry' ] Next TopicArrow-function-in-javascript |
We will understand how to check for undefined value in JavaScript in this article. Let us comprehend what an undefined value is. Undefined The "undefined" is a global property that represents a primitive type value. It is utilized to represent the absence of any value. When a declared...
3 min read
The array is the most used and essential function for displaying, operating, and handling data. We can get the same array and its values using JavaScript functions, methods, and operators. This article describes various methods for creating a clone of the original array in JavaScript. How to...
11 min read
JavaScript is a high level and versatile programming language primarily used for web development. It has grown over time to be more powerful, ultimately becoming a sophisticated programming language strong enough to drive full-fledged applications. This article takes a deep dive into the top 10 use...
9 min read
What is ? In JavaScript, the triple-equal operator tests for strict equality. The triple equals will not do the type conversion if there are two values that are not of the same type comparison. In the case of "===," it will return false. It means that the...
3 min read
In JavaScript, a fixed collection of named values is represented via enums. They enable programmers to create a list of named constants that may be used across codebase to improve error ention, readability, and maintainability. Enumerations, often known as enums, are used in JavaScript to express a...
13 min read
In 2008, Google evolved the V8 engine to improve JavaScript velocity in Google Chrome, the business enterprise's online browser. Large-scale tasks had been formerly concept to require JavaScript to be slow and inefficient, especially when in comparison to other programming languages like Java or C. Before...
10 min read
A relatively simple data-interchange format known as JSON (JavaScript Object Notation) is essential to contemporary web development. It makes data structuring simpler and more readable for humans, which makes it suited for information transfer between a client and a server. Because of its ease of use...
7 min read
In this article, we are going to discuss JavaScript's in operator. We have heard and discussed many times about arithmetic, logical, comparison, and other operators. But have you heard or know about the in operator of JavaScript. Probably the answer is no, so let's discuss...
3 min read
In JavaScript, we can hide the elements using the style.display or by using the style.visibility. The visibility property in JavaScript is also used to hide an element. The difference between the style.display and style.visibility is when using visibility: hidden, the tag is not visible, but...
2 min read
The date is an essential function for user interaction form, table and database. We can work on the date in the format of the user's requirement. The users mostly use the following date format to display the date. dd/mm/yyyy format mm/dd/yyyy format yyyy/dd/mm format dd/month_name /yyyy format Methods to Convert...
5 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