xxxxxxxxxx
// example:
let yourArray = [2, 3, 4];
yourArray.unshift(1); // yourArray = [1, 2, 3, 4]
// syntax:
// <array-name>.unshift(<value-to-add>);
xxxxxxxxxx
const array = [2, 3, 4];
const elementToAdd = 1;
array.unshift(elementToAdd);
console.log(array); // Output: [1, 2, 3, 4]
xxxxxxxxxx
//Add element to front of array
var numbers = ["2", "3", "4", "5"];
numbers.unshift("1");
//Result - numbers: ["1", "2", "3", "4", "5"]
xxxxxxxxxx
In JavaScript, you use the unshift() method
to add one or more elements to the beginning
of an array and it returns the array's
length after the new elements have been added.
example:
var colors = ['white', 'blue'];
colors.unshift('red');
console.log(colors);
// colors = ['red', 'white', 'blue']
var numbers = [2, 3, 4, 5];
numbers.unshift(1);
console.log(numbers);
// numbers: [ 1, 2, 3, 4, 5 ]
xxxxxxxxxx
var arr = [23, 45, 12, 67];
arr = [34, arr]; // RESULT : [34,23, 45, 12, 67]
console.log(arr)