The shift()
method of an array is used to remove and return the first element of the array. It is similar to the shift()
method of JavaScript, which is a subset of TypeScript.
array.shift()
array
: This is the array that we want to remove the first element of.
The value returned is the removed element of the array.
// create arrays in TypeScriptlet names: string[] = ["Theodore", "James", "Peter", "Amaka"]let numbers : Array<number>;numbers = [12, 34, 5, 0.9]let cars : Array<string> = ["Porsche", "Toyota", "Lexus"]// remove and print first elements of the arraysconsole.log("removed element: ", names.shift())console.log("removed element: ", numbers.shift())console.log("removed element: ", cars.shift())// print modified arraysconsole.log("\nModified Arrays:")console.log(names)console.log(numbers)console.log(cars)
shift()
method on the arrays, which removes and returns the first elements of the arrays. We then print the removed elements.