What is a shift in JavaScript

Array push, pop, shift, unshift

array.pop ()

Javascript array.pop () removes the last element from the array and returns this element.

  • If the array is empty, the return value is "undefinied".
  • If the last element is an array, pop () completely removes the element and returns an array.
const trees = ["coconut palm", "red book", "birch", "maple tree", ["blue fir", "Nordmann fir", "spruce"]]; let lastElem = trees.pop (); console.log ("trees" + trees); console.log ("last" + lastElem); console.log ("Type of lastElem" + typeof (lastElem)); [Log] trees Coconut palm, red book, birch, maple tree [Log] load Blue fir, Nordmann fir, spruce [Log] typeof last object

With array.pop () Javascript offers a FIFO procedure (first in, first out).

array.push ()

array.push () inserts one or more elements at the end of the array and returns the new length of the array.

  • When push inserts multiple elements, each of these elements is inserted in the order in which it is noted in the call.
  • If an element inserted with push is an array, array.push () inserts this array as an element.
let nums = ["ONE", "TWO", "THREE"]; let len ​​= nums.push ("FOUR", ["GREEN", "BLUE", "YELLOW"]); console.log ("nums" + nums); console.log ("nums len" + len); console.log ("nums [5,2]" + nums [5,2]);

nums.push ("FOUR", ["GREEN", "BLUE", "YELLOW"]) adds the elements "FOUR" and "[" GREEN "," BLUE "," YELLOW "]" to the end of the array nums one and gives in len returns the new length of the array.

[Log] nums ONE, TWO, THREE, FOUR, GREEN, BLUE, YELLOW [Log] nums len 5 [Log] nums [5] [2]

With array.push () Javascript offers a LIFO procedure (last in, first out).

Even though push () can insert an array into an array - to connect the two arrays together is array.concat () the efficient method of choice. With push (), each element would have to be appended to the first array individually in a loop or iteration.

array.shift ()

shift () and unshift () work equivalent to push and pop, but not at the end of the array, but at the beginning.

array.shift () Javascript removes the first element of the array and lets the following elements slide forward by one index - the elements of the array are shifted to the left and the first element falls out of the array.

  • If the first element of the array itself is an array, shift () returns the array to the return value.
let colors = [["Lemon Yellow", "Scarlet", "Sky Blue"], "Gray", "Green", "Blue"]; let elem = colors.shift (); console.log ("colors" + colors); console.log ("elem" + elem); console.log ("colors.length" + colors.length);

assigns the value of the first element of the variable elem and removes the first element from the array colors.

[Log] colors Gray, green, blue [log] elem Lemon yellow, scarlet red, sky blue [Log] colors.length 3

array.unshift ()

unshift () works like array.push (), but starts at the beginning of the array. array.unshift () can insert one or more elements at the beginning of the array.

Passes the call unshift () multiple arguments, the elements are inserted from right to left at the beginning of the array.

let cars = ["Opel", "Ford", "Fiat"]; let newcars ​​= cars.unshift ("Renault", "Peugeot"); console.log ("cars" + cars); console.log ("cars.length" + cars.length); [Log] cars Renault, Peugeot, Opel, Ford, Fiat [Log] cars.length 5

External sources