Skip to content

New Array methods in JavaScript.

Published: at 10:44 AM (1 min read)

with() method - It is used to assign an existing array to a new array

var arr = [1, 6, 3, 4, 5];

var newArr = arr.with(1, 2);

console.log(newArr); // [1, 2, 3, 4, 5]

toSorted() method - It is used to sort the elements of an array.

var arr = [1, 5, 3, 2, 4];

var sortedArr = arr.toSorted();

console.log(sortedArr); // [1, 2, 3, 4, 5]

toReversed() method - It is used to reverse the order of the elements of an array.

var arr = [1, 5, 3, 2, 4];

var reversedArr = arr.toReversed();

console.log(reversedArr); // [5, 4, 3, 2, 1]

toSpliced() method - It is used to remove and/or add elements to an array.

var arr = [1, 5, 3, 2, 4];

var newArr = arr.toSpliced(0, 2);

console.log(newArr); // [3, 2, 4]