unshift

Add an element to the front of an array.

listinsertat beginning
unshift(value: T): number;

You might have an array with 3 numbers, like 1, 2, and 3. If you want to add another number to the front, then you unshift it into the array. You do it like this:

let thoseNumbers = [1, 2, 3];
let head = 0;
head = thoseNumbers.unshift(0);

Parameters

  • item: the element to add to the front of the array.

Example

Make an array that simulates a train. Place the parts of the train in the right order.

array offlatcarboxcartankerengineflatcarcabooseboxcarpartslength of array0>partsget and remove first value fromifthenelsepartslength of array1>ifthenelse ifthenelsepartengine=partcaboose=partspartadd valueto endtrainpartadd valueto endtrainpartinsertat beginningsetcounttotrainpartinsertat beginningsetcounttosetparttowhiledosetpartstoon start

Sea also

shift, push

array offlatcarboxcartankerengineflatcarcabooseboxcarpartslength of array0>partsget and remove first value fromifthenelsepartslength of array1>ifthenelse ifthenelsepartengine=partcaboose=partspartadd valueto endtrainpartadd valueto endtrainpartinsertat beginningsetcounttotrainpartinsertat beginningsetcounttosetparttowhiledosetpartstoon start