How to Add Elements to a JavaScript Array?19 Apr 2025 | 4 min read What is JavaScript?JavaScript is a very powerful and popular programming language with a lot of applications in terms of web development. It enables developers to design dynamic and interactive content for web pages for a seamless user experience. What is an Array?An array is a special kind of data type in which we can store a list of items (usually numbers, strings, or other object types, if desired) with a single variable. With different methods and properties, it is possible to manipulate the contents of an array. Example: Array.push() MethodOne of the most simple method to add an element at the end of an array is push() method. It alters the original array and returns the length of the altered array. Code: Output: [ 'red', 'green', 'blue', 'yellow' ] Spread Operator with Array LiteralAnother way is by creating a new array using the spread operator (...) and pushing new elements. This method doesn't change the original array. Code: Output: [ 1, 2, 3, 4, 5 ] JavaScript splice() MethodYou can use the splice() method to insert elements at any particular index of the array. It mutates the original array removes elements or adds new ones. Syntax:
Code: Output: [ 'cat', 'elephant', 'lion', 'dog', 'rabbit' ] JavaScript concat() MethodThe concat() method is used to join two or more arrays or to add new elements to an existing array. Won't change the original array but going to return a new one. Code: Output: Merged Array is :: [ 10, 20, 30, 40, 50 ] Original Array is :: [ 10, 20 ] Advantages
Disadvantages
ConclusionJavaScript has different ways to add elements in an array and each way can match the certain case. Push() and splice() mutate the array, while spread operator and concat() create a new array. With knowledge of these techniques, you can pick the proper way based on your use case and write cleaner, effective code. Next TopicJavascript Array Max |
JavaScript rearmost interpretation of latest version is a new utility in programming that resembles special times of time with the proposed highlights of ES2024, contrivers are near the precarious edge of opening up different advancements that guarantee to make rendering in JavaScript more complete, lucid,...
3 min read
We will understand the in this article. What is currency? It is the type of money which is utilized by a particular country that means each country has a specific currency which consists of different patterns. There are different methods that are utilized to display a...
3 min read
In earlier times, JavaScript used to run in the client's browser. As the technology evolved with the time, web development also evolved. Websites were static in older times which means they only showed information but now they have become dynamic consisting of many things to interact...
5 min read
What is Name validation in JavaScript? In JavaScript, name validation is a crucial factor of form validation in web development. With the usage of name validation in JavaScript, we make sure that the data entered using the users follows the basic requirements, ents errors and additionally...
3 min read
The trim() is a built-in string function in JavaScript, which is used to trim a string. This function removes the whitespace from both the ends, i.e., start and end of the string. As the trim() is a string method, so it is invoked by an...
3 min read
When it comes to web development, JavaScript is one of the most popular and adaptable programming languages. Because of its adaptability, it is a fundamental component of contemporary web development, enabling developers to create dynamic and interactive web pages. The ability to extract meaning from code...
9 min read
We will understand the in this article. ASCII It stands for the American Standard Code for Information Interchange. It is a character encoding format utilized to communicate between computers. It provides standard numeric values to numerals, letters, etc. There are 128 unique characters in ASCII. It contains...
3 min read
At first, coding can seem difficult especially for beginners. This article aims to assist you in simplifying matters. This article will guide the process of “commenting out multiple lines in JavaScript”. Mastering the skill of commenting on your code is crucial for every programmer. This method...
4 min read
The jQuery JavaScript library offers jQuery DataTable, an intelligent and powerful plugin for upgrading HTML tables. It is an extremely adaptable application designed to improve data accessibility in HTML tables by displaying information in tables and adding interactions. jQuery DataTables makes it easy to add sophisticated functionality...
13 min read
While both JavaScript and ReactJS are crucial for contemporary web development, their capabilities are incredibly unique. It is fundamental for developers to understand these distinctions to make versatile, dynamic, and viable online apps. This article investigates the key qualifications, applications, and advantages among React.js and JavaScript. What...
11 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