Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

Array Methods in JavaScript Part 2

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
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Array Methods in JavaScript Part 2

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
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

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 = fruits.push("grape",
"pineapple");
console.log(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 = fruits.pop();
console.log(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 = fruits.unshift("apple",
"grape");
console.log(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 = fruits.splice(1,


1, 'peach',);

console.log(fruits);
// Output: ['apple', 'peach']
console.log(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 = fruits.slice(2, 4);
console.log(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"];

fruits.reverse();
console.log(fruits);

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


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

FOLLOW FOR MORE

You might also like