Adding and Removing Elements
push - adds an element to the end of an array.
Copy let array = [ 1 , 2 , 3 , 4 , 5 ] ;
array . push ( 6 ) ;
console . log ( array ) ; // [1,2,3,4,5,6] pop - removes the element at the end of the array. returns the element.
Copy let array = [ 1 , 2 , 3 , 4 , 5 ] ;
let element = array . pop () ;
console . log ( array ) ; // [1,2,3,4]
console . log ( element ) ; // 5 unshift - adds an element to the beginning of an array
Copy let array = [ 1 , 2 , 3 , 4 , 5 ] ;
array . unshift ( 0 ) ;
console . log ( array ) ; // [0,1,2,3,4,5,6] shift - removes an element from the beginning of an array. returns the element.
Copy let array = [ 1 , 2 , 3 , 4 , 5 ] ;
let element = array . shift () ;
console . log ( array ) ; // [2,3,4,5]
console . log ( element ) ; // 1 Splicing Elements
splice - combines adding and removing elements in a single method. This method is difficult to learn all the options for the parameters, but you can just focus on the simple use-cases, which are the majority, and it's not too hard.
start: at which index to start the change
deleteCount: the number of elements to remove
items: a comma-separated list of elements to add
Splicing in a single value
Splicing in multiple values
Removing an element in the middle
Replace an element in the middle