- toString() - converts array to string with ,
- join() - joins with delimeter and converts to string
- pop()
- push()
- shift()
- unshift()
Copy
// Array Methods
let num = [1,2,3,34,7];
console.log(num)
// toString
let b = num.toString() // b is a string
console.log(b)
// join
let c = num.join("_")
console.log(c, typeof c)
// pop
let d = num.pop() // removing last element of array
console.log(d , num)
// push
let e = num.push("hello") // return new array length
console.log(e, num)
// shift
let f = num.shift() // removes first eleemt
console.log(f,num)
// unshift
let g = num.unshift("kiran") // adds at starting
console.log(g,num)
- delete - Operator
- concat
- sort
- reverse
- splice
- slice
Copy
// delete
console.log(num, num.length)
delete num[0]
console.log(num, num.length)
// concat -> Join multiple arrays
let num2 = [6,7,89,0]
let newArray = num.concat(num2,num,num2) // Not modifying old array
console.log(newArray)
// sort method
let unsor = [ 3 ,4,5,9,1,2,0,123]
unsor.sort() // string sort
console.log(unsor)
// sort takes a argument called compare function
let compare = (a,b)=>{
return a-b // b - a for decesding order
}
unsor.sort(compare)
console.log(unsor)
// reverse
unsor.reverse()
console.log(unsor)
// Splice and Slice
// Splice -> to add new elements
// 3 args -> where to add , how many to delete , values to add
let deleted_values =num.splice(2,2,898989898,111111,909090)
console.log(deleted_values,num)
// Slice
// cuts and creates a array
console.log(num.slice(2,4),num)
console.log(num.slice(2),num)