insert element at beginning of array javascript
//The unshift() method adds one or more elements to the beginning of an array
//and returns the new length of the array.
const array1 = [1, 2, 3];
console.log(array1.unshift(4, 5));
// expected output: 5
console.log(array1);
// expected output: Array [4, 5, 1, 2, 3]var name = [ "john" ];
name.unshift( "charlie" );
name.unshift( "joseph", "Jane" );
console.log(name);
//Output will be
[" joseph "," Jane ", " charlie ", " john "]// Use unshift method if you don't mind mutating issue
// If you want to avoid mutating issue
const array = [3, 2, 1]
const newFirstElement = 4
const newArray = [newFirstElement].concat(array) // [ 4, 3, 2, 1 ]
console.log(newArray);
Also in JavaScript:
- angular 8 remove cookies
- not getting webhook post request json data in my laravel controller
- if else jquery click function
- nexusjs file upload
- how to append element in array angular
- .remove javascript
- buttons js before submit
- discord.js start code
- button not exist js
- iframe video speed
- javascript sort chars in string
- javascript replace string
- ctx.fillstyle
- javascript function variable arguments
- jquery get request with headers
- javascript array group by id
- How do you select a particular option in a SELECT element in jQuery?
- javascript checkbox value
- react.createref()
- uuid react
- angular cdk media query
- javascript prototype vs constructor function
- js function
- cordova load javascript without cache