How to append an item into a JavaScript array?

To append an item to a JavaScript array, you can use several methods. The most common approach is the push() method, which adds elements to the end of an array.

Using push() Method

The push() method adds one or more elements to the end of an array and returns the new length:

<html>
   <body>
      <script>
         var arr = ["marketing", "technical", "finance", "sales"];
         arr.push("HR");
         document.write(arr);
      </script>
   </body>
</html>
marketing,technical,finance,sales,HR

Adding Multiple Items

You can append multiple items at once using push():

<html>
   <body>
      <script>
         var departments = ["marketing", "technical"];
         departments.push("finance", "sales", "HR");
         document.write(departments);
      </script>
   </body>
</html>
marketing,technical,finance,sales,HR

Using Spread Operator

The spread operator provides a modern way to append items:

<html>
   <body>
      <script>
         var originalArray = ["apple", "banana"];
         var newArray = [...originalArray, "cherry", "date"];
         document.write("Original: " + originalArray + "<br>");
         document.write("New: " + newArray);
      </script>
   </body>
</html>
Original: apple,banana
New: apple,banana,cherry,date

Using concat() Method

The concat() method creates a new array by combining existing arrays:

<html>
   <body>
      <script>
         var fruits = ["apple", "banana"];
         var moreFruits = fruits.concat(["cherry"], "date");
         document.write("Original: " + fruits + "<br>");
         document.write("Combined: " + moreFruits);
      </script>
   </body>
</html>
Original: apple,banana
Combined: apple,banana,cherry,date

Comparison

Method Modifies Original Returns Use Case
push() Yes New length Add to existing array
Spread operator No New array Create new array
concat() No New array Combine arrays

Conclusion

Use push() to modify the original array, or the spread operator and concat() when you need to create a new array without changing the original.

Updated on: 2026-03-15T23:18:59+05:30

304 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements