Dave's Brain

Browse - Programming Tips - How can I prepend (add at the start) an element at the start of a javaScript array?

Date: 2016apr17
Language: javaScript
Keywords: append

Q.  How can I prepend (add at the start) an element at the start of a javaScript array?

A.  Use unshift() like this:

var a = [ 'three', 'four', 'five' ]; a.unshift(['one', 'two']); // prepend an array at the start a.unshift('zero'); // prepend just one element

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.