.unshift()
Anonymous contributors
Anonymous contributors
Anonymous contributors
Published Jun 21, 2021Updated Sep 3, 2021
Contribute to Docs
Adds one or more elements to beginning of array and returns new length.
Syntax
array.unshift(item1, item2, ...);
Examples
To add the element 'Monday'
to the daysOfWeek
array:
const daysOfWeek = ['Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday',];daysOfWeek.unshift('Monday');console.log(daysOfWeek);// Output: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
To add elements 1
and 2
to the countToTen
array:
const countToTen = [3, 4, 5, 6, 7, 8, 9, 10];countToTen.unshift(1, 2);console.log(countToTen);// Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
All contributors
- Anonymous contributorsAnonymous contributors
- Anonymous contributors
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.