Languages
[Edit]
EN

JavaScript - add item at the beginning of array

3 points
Created by:
Adnaan-Robin
724

In this article, we would like to show you how to add item at the beginning of array using JavaScript.

Quick solution:

array.unshift(item);  // prepends item

 

Practical example

// ONLINE-RUNNER:browser;

var array = ['A', 'B'];

array.unshift('C');  // prepends item

console.log(array);  // ['C', 'A', 'B']

 

See also

  1. JavaScript - append item to array

References

  1. Array.prototype.unshift() - JavaScript | MDN

Alternative titles

  1. JavaScript - add element at the beginning of array
  2. JavaScript - add new array elements at the beginning of array
  3. JavaScript - prepend item to array
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join