Programming Tips - javaScript: prepend (add at the start) at the start of an array

Date: 2016apr17 Update: 2025sep2 Language: javaScript Keywords: append, efficient Q. javaScript: prepend (add at the start) at the start of an array A. Use unshift() like this:
let a = [ 'three', 'four', 'five' ]; // Start with an example array a.unshift('zero'); // Prepend just one element a.unshift(['one', 'two']); // Prepend an array at the start
The member function name `unshift()` sounds odd at first. But it makes more sense when you know there is a corresponding `shift()` member function. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift