codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🌐

JavaScript

26 / 65 topics
21JavaScript Strings22JavaScript String Methods23JavaScript Numbers24JavaScript Math Object25JavaScript Arrays26JavaScript Array Methods27JavaScript Array Iteration
Tutorials/JavaScript/JavaScript Array Methods
🌐JavaScript

JavaScript Array Methods

Updated 2026-05-12
30 min read

JavaScript Array Methods

In the previous topic, we covered the basics of arrays in JavaScript. Now, let's dive deeper into how you can manipulate arrays using various methods. These methods are essential for adding, removing, and modifying elements within an array. Understanding these methods will greatly enhance your ability to work with data efficiently.

Introduction

JavaScript provides a rich set of built-in methods that allow you to easily manipulate arrays. Whether you need to add or remove elements, create new arrays from existing ones, or join them into strings, there's a method for it. In this tutorial, we'll explore some of the most commonly used array methods: push, pop, shift, unshift, splice, slice, concat, and join.

Core Content

1. Adding Elements

push

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

pushExample.js
1let fruits = ['apple', 'banana'];
2let newLength = fruits.push('cherry');
3console.log(fruits); // Output: ['apple', 'banana', 'cherry']
4console.log(newLength); // Output: 3
Output
['apple', 'banana', 'cherry']
3

unshift

The unshift method adds one or more elements to the beginning of an array and returns the new length of the array.

unshiftExample.js
1let fruits = ['apple', 'banana'];
2let newLength = fruits.unshift('mango');
3console.log(fruits); // Output: ['mango', 'apple', 'banana']
4console.log(newLength); // Output: 3
Output
['mango', 'apple', 'banana']
3

2. Removing Elements

pop

The pop method removes the last element from an array and returns that element.

popExample.js
1let fruits = ['apple', 'banana', 'cherry'];
2let removedElement = fruits.pop();
3console.log(fruits); // Output: ['apple', 'banana']
4console.log(removedElement); // Output: 'cherry'
Output
['apple', 'banana']
'cherry'

shift

The shift method removes the first element from an array and returns that element.

shiftExample.js
1let fruits = ['mango', 'apple', 'banana'];
2let removedElement = fruits.shift();
3console.log(fruits); // Output: ['apple', 'banana']
4console.log(removedElement); // Output: 'mango'
Output
['apple', 'banana']
'mango'

3. Modifying Elements

splice

The splice method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.

spliceExample.js
1let fruits = ['apple', 'banana', 'cherry'];
2fruits.splice(1, 1, 'orange'); // Remove one element at index 1 and add 'orange'
3console.log(fruits); // Output: ['apple', 'orange', 'cherry']
Output
['apple', 'orange', 'cherry']

slice

The slice method returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included).

sliceExample.js
1let fruits = ['apple', 'banana', 'cherry'];
2let slicedFruits = fruits.slice(1, 3);
3console.log(slicedFruits); // Output: ['banana', 'cherry']
Output
['banana', 'cherry']

4. Combining Arrays

concat

The concat method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.

concatExample.js
1let fruits = ['apple', 'banana'];
2let vegetables = ['carrot', 'broccoli'];
3let combinedArray = fruits.concat(vegetables);
4console.log(combinedArray); // Output: ['apple', 'banana', 'carrot', 'broccoli']
Output
['apple', 'banana', 'carrot', 'broccoli']

5. Converting Arrays to Strings

join

The join method creates and returns a new string by concatenating all the elements in an array, separated by commas or a specified separator.

joinExample.js
1let fruits = ['apple', 'banana', 'cherry'];
2let joinedString = fruits.join(', ');
3console.log(joinedString); // Output: 'apple, banana, cherry'
Output
'apple, banana, cherry'

Practical Example

Let's create a practical example that uses several of these methods to manage a list of tasks.

taskManager.js
1let tasks = ['Buy groceries', 'Read a book'];
2
3// Add new tasks
4tasks.push('Write code');
5tasks.unshift('Plan weekend');
6
7console.log('Tasks after adding:', tasks);
8// Output: Tasks after adding: ['Plan weekend', 'Buy groceries', 'Read a book', 'Write code']
9
10// Remove a task
11let removedTask = tasks.pop();
12console.log('Removed task:', removedTask);
13console.log('Tasks after removing:', tasks);
14// Output: Removed task: Write code
15// Tasks after removing: ['Plan weekend', 'Buy groceries', 'Read a book']
16
17// Modify a task using splice
18tasks.splice(1, 1, 'Buy fresh groceries');
19console.log('Tasks after modifying:', tasks);
20// Output: Tasks after modifying: ['Plan weekend', 'Buy fresh groceries', 'Read a book']
21
22// Join tasks into a single string
23let taskString = tasks.join(', ');
24console.log('Task list as a string:', taskString);
25// Output: Task list as a string: Plan weekend, Buy fresh groceries, Read a book
Output
Tasks after adding: ['Plan weekend', 'Buy groceries', 'Read a book', 'Write code']
Removed task: Write code
Tasks after removing: ['Plan weekend', 'Buy groceries', 'Read a book']
Tasks after modifying: ['Plan weekend', 'Buy fresh groceries', 'Read a book']
Task list as a string: Plan weekend, Buy fresh groceries, Read a book

Summary

MethodDescription
pushAdds elements to the end of an array.
popRemoves the last element from an array.
shiftRemoves the first element from an array.
unshiftAdds elements to the beginning of an array.
spliceChanges the contents of an array by removing or replacing existing elements and/or adding new elements in place.
sliceReturns a shallow copy of a portion of an array into a new array object selected from start to end (end not included).
concatMerges two or more arrays.
joinCreates and returns a new string by concatenating all the elements in an array, separated by commas or a specified separator.

What's Next?

In the next topic, we'll explore how to iterate over arrays using various methods like forEach, map, filter, and reduce. These methods will help you perform operations on each element of an array efficiently.

Stay tuned for more JavaScript tutorials!


PreviousJavaScript ArraysNext JavaScript Array Iteration

Recommended Gear

JavaScript ArraysJavaScript Array Iteration