DEV Community

Siddharth Kanojiya
Siddharth Kanojiya

Posted on

Some More JavaScript Array Methods #15

// delete Object
// let num = [1, 2, 3, 4, 5, 6, 7, 8]
// console.log(num.length)
// delete num[0]
// console.log(num)

// concat
// let num_more = [11, 21, 31, 41, 51, 61, 71, 81]
// let newArray = num.concat(num_more)
// console.log(newArray)
// console.log(num,num_more)

// sort method
// let num = [169, 2, 35, 4, 65, 61, 70, 298]
// num.sort()
// console.log(num)

/*sort() take an optional compare function .if this function is provided as the first argument the sort() function will consider these value (the value returned from the compare function) as the basis of sorting */

// for Example

// let compare = (a, b)=>{
// return a - b
// }
// let num = [169, 2, 35, 4, 65, 61, 70, 298]
// num.sort(compare)
// console.log(num)

// // reverse() it will reverse the array

// splice (splice can be used to add new item to an array )
//slice (slice out a piece from an array ,it create a new array )

let num = [169, 2, 35, 4, 65, 61, 70, 298]
// num.splice(2, 4, 787, 998, 945, 66,)
// let deletedValue = num.splice(2, 4, 787, 998, 945, 66,)
// console.log(num)
// console.log(deletedValue)

// let numNew = num.slice(3)
let numNew = num.slice(3, 5)
console.log(numNew)

Top comments (0)