Array methods (push(), pop(), shift(), unshift(), splice(), slice())

Array methods (push(), pop(), shift(), unshift(), splice(), slice())

JavaScript programming provides programmers with several built-in functions and methods to manipulate the created array. These built-in array methods allow programmers to add new elements to an existing array, delete and modify existing array elements.

Array methods (push(), pop(), shift(), unshift(), splice(), slice())

So, let’s get to know more about the multiple array methods used in JavaScript programming. These include push(), pop(), shift(), unshift(), splice(), and slice() array methods.

JavaScript push() array method.

The push() method in JavaScript array programming helps to add one or more elements to the end of an existing array, and increase the new length of the existing array.

Syntax of JavaScript push() array method.

array.push(element1, element2, …, elementN);

Example of push() array method.

let course = [“o level”, “A level”];

let incorder = course.push(“b level”, “c level”);

console.log(course); // Result is – [ ‘o level’, ‘A level’, ‘b level’, ‘c level’ ]

console.log(incorder); // Result is – new increase order or array is 4

Explanation of push() array method.

In this program, push() array method adds “b level” and “c level” array elements at the end of course array.

JavaScript pop() array method.

In JavaScript array program, pop() array method deletes the last array element from an existing array and displays the array element.

Syntax of pop() array method.

let lastElement = array.pop();

Example of pop() array method.

let course = [“o level”, “A level”,”b level”, “c level”];

let decorder = course.pop();

console.log(course); // Result is – [ ‘o level’, ‘A level’, ‘b level’ ]

console.log(decorder); // Result is – c level

Explanation of pop() array method.

In this program, pop() array method deletes the last element of the array in this condition “c level” and displays the remaining array elements.

JavaScript shift() array method.

In the JavaScript program, shift() array method deletes the first element from an array and returns the array value. In the array shift process, all other array elements are also moved to lower indexes.

Syntax of shift() array method.

let firstElement = array.shift();

Example of shift() array method.

let course = [“o level”, “A level”,”b level”, “c level”];

let element1 = course.shift();

console.log(course); // Result is – [ ‘A level’, ‘b level’, ‘c level’ ]

console.log(element1); // Result is – o level

Explanation of shift() array method.

In this program, shift() array method displays the array value by deleting the first element of the array in this condition “o level”.

JavaScript unshift() array method.

In the JavaScript program, unshift() array method helps to add one or more array elements to the start of the array, and displays the new list value of the current array.

Syntax of unshift() array method.

let newLength = array.unshift(element1, element2, …, elementN);

Example of unshift() array method.

let course = [“o level”, “A level”,”b level”, “c level”];

let unshiftelement = course.unshift(“mca”, “bca”);

console.log(course); // Result is – [ ‘mca’, ‘bca’, ‘o level’, ‘A level’, ‘b level’, ‘c level’ ]

console.log(unshiftelement); // Result is – 6 increased array length

Explanation of unshift() array method.

In this program, unshift() array method adds new array elements “mca” and “bca” to the start of the existing array. And now the new length of the existing array is 6.

JavaScript splice() array method.

In JavaScript program, splice() array method modifies the default content of the existing array by deleting, replacing or adding array elements at a particular array index.

Syntax of splice() array method.

array.splice(startIndex, deleteCount, element1, element2, …, elementN);

  • startIndex – This is the index in the existing array from where to start the manipulation of the array.
  • deleteCount – The number of array elements to delete from the current array. If 0, no array elements are deleted from the current array.
  • element1, element2, …, elementN – These are the array elements to be added to the current array, starting from startIndex.

Example of removing elements from an array using the splice() array method.

let course = [“o level”, “A level”,”b level”, “c level”];

let removeelement = course.splice(1, 2);

console.log(course); // Result is – [ ‘o level’, ‘c level’ ]

console.log(removeelement); // Result is – [ ‘A level’, ‘b level’ ]

Explanation of the splice() array method.

This program displays the splice() array method removing the elements “A level” and “b level'” from the array starting from index 1.

Example of adding array elements.

let course = [“o level”, “a level”, “b level”, “c level”];

course.splice(1, 0, “mca”, “bca”); // here it Adds “mca” and “bca” starting at index 1 location

console.log(course); // Result – [ ‘o level’, ‘mca’, ‘bca’, ‘a level’, ‘b level’, ‘c level’ ]

course.splice(1, 0, “mca”, “bca”); // here it Adds “mca” and “bca” starting at index 1 location

console.log(course); // Result – [‘o level’, ‘mca’,’bca’, ‘mca’,’bca’, ‘a level’,’b level’, ‘c level’]

Explanation of adding array elements.

Here in this program, the splice() array method adds “mca” and “bca” at index location 1 without deleting any existing array element.

Example of replacing an existing array element.

let course = [“o level”, “a level”, “b level”, “c level”];

course.splice(1, 1, “mca”, “bca”); // it Removes “a level” and adds “mca”, “bca”

console.log(course); // result is – [ ‘o level’, ‘mca’, ‘bca’, ‘b level’, ‘c level’ ]

Explanation of replacing array element.

In this program, the splice() array method removes “a level” and replaces it with “mca” and “bca” array elements.

JavaScript slice() array method.

In the JavaScript program, the slice() method returns a shallow copy of a part of the array as a new array value. It does not modify your original array.

Syntax of slice() array.

let newArray = array.slice(beginIndex, endIndex);

  • beginIndex – index (inclusive) to start the slice in the existing array.
  • endIndex – index (exclusive) to end the slice in an array. If omitted, it slices till the end of the array.

Example of copying a part of array.

let course = [“o level”, “a level”, “b level”, “c level”];

let sliced = course.slice(1, 3); // here it Extracts elements from index 1 to 2 location

console.log(sliced); // Result – [ ‘a level’, ‘b level’ ]

Explanation of slice() array method.

In this program, slice() array method displays a new array containing array elements from index 1 to 2 excluding index 3.

Example of copying an entire array.

let course = [“o level”, “a level”, “b level”, “c level”];

let copycourse = course.slice();

console.log(copycourse); // Result – [ ‘o level’, ‘a level’, ‘b level’, ‘c level’ ]

Explanation of copying an entire array.

In this program, when no argument is given to the slice() method, it returns a shadow copy array value of the complete array element.

Summary of array methods in JavaScript programming.

  • push() – This JavaScript program adds an element to the end of the array, and returns the length value of the new array value.
  • pop() – This program deletes the last array element from the array and displays the array value.
  • shift() – This program deletes the first element from the array and displays the array value.
  • unshift() – Adds a new array element to the start of a JavaScript array, and displays the length of the new array value.
  • splice() – Modifies an array by deleting, replacing, or adding an array element at a specific index.
  • slice() – Displays a shadow copy of a portion of an array, without modifying the original array in a JavaScript program.

Leave a Reply