I have already created a array dynamically using Ajax, but now when i am getting new values to server, sometime i need to add that array on first position based on requirements, so how can i add a new element of an array to first position using javascript(preferred) or jQuery?
Unshift and Shift are the functions which can be used to add or remove elements from the array starting point, while Push and pop are the functions which is used to add element or remove relement at the end of the array
so basically what you need is
var a = [12, 24, 50, 100]; //this is your array
a.unshift(11); // add element add the start of an array
console.log(a); // this will be your result [11,12, 24, 50, 100]
here is a simple diagram created by me
hope the aboave helps to understand functions to add or remove elements from arrays in javascript
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly