[Go to site: main page, start]

0% found this document useful (0 votes)
7 views8 pages

JavaScript Array Push Method Explained

The document discusses several array methods in JavaScript including push(), pop(), unshift(), splice(), slice(), and reverse(). Examples are provided for each method showing how they can be used to modify arrays in different ways.

Uploaded by

aryabhatkande444
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views8 pages

JavaScript Array Push Method Explained

The document discusses several array methods in JavaScript including push(), pop(), unshift(), splice(), slice(), and reverse(). Examples are provided for each method showing how they can be used to modify arrays in different ways.

Uploaded by

aryabhatkande444
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

AKSHAT GARG

Array
Methods
in
JavaScript
PART -2
Push Method
In JavaScript, the push() method is an array
method that adds one or more elements to
the end of an array and returns the new
length of the array. Example :-

let fruits = ["apple", "banana",


"orange"];
let length = [Link]("grape",
"pineapple");
[Link](fruits);

// Output: ["apple", "banana",


"orange", "grape", "pineapple"]
Pop Method
In JavaScript, the pop() method is an array
method that removes the last element from
an array and returns that element. Example :

let fruits = ["apple", "banana",


"orange"];
let lastFruit = [Link]();
[Link](fruits);

// Output: ["apple", "banana"]


Shift Method
In JavaScript, the unshift() method is an
array method that adds one or more
elements to the beginning of an array and
returns the new length of the array.

let fruits = ["banana", "orange"];


let length = [Link]("apple",
"grape");
[Link](fruits);

// Output: ["apple", "grape",


"banana", "orange"]
Unshift Method
The splice() method in JavaScript changes the
contents of an array by removing or replacing
existing elements and/or adding new elements in
place. It modifies the original array and returns an
array of the removed elements, if any. Example :

const fruits = ['apple', 'banana'];

const removedFruits = [Link](1,


1, 'peach',);

[Link](fruits);
// Output: ['apple', 'peach']
[Link](removedFruits);
// Output: ['banana']
Slice Method
In JavaScript, the slice() method is an array
method that returns a shallow copy of a portion
of an array into a new array, without modifying
the original array.

let fruits = ["apple", "banana",


"orange", "grape", "pineapple"];
let citrus = [Link](2, 4);
[Link](citrus);

// Output: ["orange", "grape"]


Slice Method
In JavaScript, the reverse() method is an array
method that reverses the order of the elements
in an array in place (modifies the original array).

let fruits = ["apple", "banana",


"orange", "grape", "pineapple"];

[Link]();
[Link](fruits);

// Output: ["pineapple", "grape",


"orange", "banana", "apple"]
Akshat Garg

FOLLOW FOR MORE

Common questions

Powered by AI

Modifying the original array using 'splice' and 'reverse' has significant implications, especially in programs where data immutability is crucial. The 'splice' method changes the array by removing, replacing, or adding elements, which can alter data integrity if not carefully managed. In scenarios where arrays are referenced elsewhere in the code or shared across different application components, using 'splice' can lead to unexpected side effects as it acts destructively on the original array. Similarly, 'reverse' inverts the order of elements in place, permanently altering the original array's sequence. This operation directly impacts how elements are accessed thereafter, which can be problematic in loops or iterators assuming a specific order. Such modifications prevent the predictable reuse of the original data and necessitates caution or the use of shallow copies for safe transformations .

The 'splice' method in JavaScript changes the content of an array by removing, replacing, or adding elements. It modifies the original array in place and can return an array of the removed elements if any are specified. For instance, with an array const fruits = ['apple', 'banana'], applying the method fruits.splice(1, 1, 'peach') removes 'banana' and inserts 'peach' at the same position, modifying the array to ['apple', 'peach']. It returns ['banana'] as the array of removed elements .

The 'push' method adds one or more elements to the end of an array and returns the new length of the array, effectively expanding the array. For instance, if you have an array let fruits = ['apple', 'banana']; and execute fruits.push('orange'), the array becomes ['apple', 'banana', 'orange']. In contrast, the 'pop' method removes the last element of the array and returns that element, thereby reducing the size of the array by one. If you apply fruits.pop() to the previous example, the array changes back to ['apple', 'banana'], and 'orange' is returned as the removed element .

Both 'push' and 'unshift' methods add elements to an array but at different positions. The 'push' method appends elements to the end of the array, increasing its size from the back and returning the new length. In contrast, 'unshift' adds elements to the beginning of the array, resulting in all previously existing elements being shifted forward to accommodate the new beginning elements. Each method returns the new length of the array after adding the elements. For example, let fruits = ['banana']; using fruits.push('orange') results in ['banana', 'orange'], while fruits.unshift('apple') will result in ['apple', 'banana'].

The 'slice' method returns a shallow copy of a portion of an array based on specified start and end indices, without modifying the original array. For example, let fruits = ['apple', 'banana', 'orange', 'grape', 'pineapple']; and invoking fruits.slice(2, 4) returns a new array ['orange', 'grape'], leaving the original array unchanged. In contrast, the 'splice' method directly alters the original array's content by adding, removing, or replacing elements, such as when using fruits.splice(1, 1, 'peach'), which changes the original array and returns the removed elements, if any .

The 'shift' method removes the first element from an array and returns that removed element, reducing the array's length by one. As a result, the remaining elements' indices are decreased by one. For example, with an array let fruits = ['apple', 'banana', 'orange'], executing fruits.shift() results in ['banana', 'orange'] and returns 'apple'. The 'unshift' method, on the other hand, adds one or more elements to the beginning of an array and returns the new length of the array. Using the same array, if you execute fruits.unshift('grape'), the array becomes ['grape', 'apple', 'banana', 'orange'], where 'grape' is added at the beginning .

A practical use of the 'splice' method is when there is a need to both remove certain elements and add new elements in place at specific positions within an array. Unlike other methods such as 'push', 'pop', 'shift', and 'unshift', which add or remove elements at the ends of an array, 'splice' allows precise control within any part of the array, modifying it directly. For example, if you have an array representing a queue or a list of priorities that needs dynamic reordering or updating, like let tasks = ['design', 'implement', 'test'], you can use tasks.splice(1, 1, 'develop') to replace 'implement' with 'develop', resulting in ['design', 'develop', 'test'], efficiently managing the task list within the same data structure .

The 'slice' method is used to create subarrays by copying a part of an array into a new array, without modifying the original array. It takes two parameters: start and end indices, and returns the elements between these indices as a new array. For example, given let fruits = ['apple', 'banana', 'orange', 'grape', 'pineapple'], invoking fruits.slice(1, 3) returns a new array ['banana', 'orange'], while the original array remains unchanged. This non-destructive usage makes 'slice' ideal for operations where the original array data needs to be preserved .

Both 'pop' and 'shift' methods are used to remove elements from an array, but they differ in terms of which element they remove and how they affect the array's structure. The 'pop' method removes the last element of an array, shrinking its length by one and returning the removed element. For example, let fruits = ['apple', 'banana', 'orange']; executing fruits.pop() results in the array ['apple', 'banana'] with 'orange' returned. Conversely, the 'shift' method removes the first element, decreasing the length of the array similarly, but shifting all other elements one index down. For the same initial array, fruits.shift() results in ['banana', 'orange'] with 'apple' returned. This distinction is crucial when the order and positions of elements in the array matter .

The 'reverse' method reverses the order of elements within an array in place, thus modifying the original array. This means that after applying the method, the first element becomes the last, the second becomes second to last, and so on. For example, if you have an array let fruits = ['apple', 'banana', 'orange', 'grape', 'pineapple'], executing fruits.reverse() changes the array to ['pineapple', 'grape', 'orange', 'banana', 'apple'].

You might also like