Search This Blog

2023/06/05

New Array methods in javascript

 let arr = ["sagar","sangram","sachin"]


//change element at specific index
var newArray = arr.with(0,"swapnil")

console.log("original:",arr)
console.log("new:",newArray)

//sort array and return sorted array but do not change original array
let sorted = arr.toSorted()
console.log("sorted:",sorted)
console.log("original:",arr)


//return reversed array but do not change original arary
let reveresed = arr.toReversed()
console.log("reveresed:",reveresed)
console.log("original:",arr)


//return spliced array but do not change original array
let spliced = arr.toSpliced(0,1)
console.log("spliced:",spliced)
console.log("original:",arr)

No comments:

Post a Comment