Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6791065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:48:04+00:00 2026-05-26T17:48:04+00:00

I have a need to add or prepend elements at the beginning of an

  • 0

I have a need to add or prepend elements at the beginning of an array.

For example, if my array looks like below:

[23, 45, 12, 67]

And the response from my AJAX call is 34, I want the updated array to be like the following:

[34, 23, 45, 12, 67]

Currently I am planning to do it like this:

var newArray = [];
newArray.push(response);

for (var i = 0; i < theArray.length; i++) {
    newArray.push(theArray[i]);
}

theArray = newArray;
delete newArray;

Is there a better way to do this? Does JavaScript have any built-in functionality that does this?

The complexity of my method is O(n) and it would be really interesting to see better implementations.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:48:05+00:00Added an answer on May 26, 2026 at 5:48 pm

    Use unshift. It’s like push, except it adds elements to the beginning of the array instead of the end.

    • unshift/push – add an element to the beginning/end of an array
    • shift/pop – remove and return the first/last element of an array

    A simple diagram…

    unshift -> [array] <- push
    shift   <- [array] -> pop
    

    and chart:

      add remove start end
    push X X
    pop X X
    unshift X X
    shift X X

    Check out the MDN Array documentation. Virtually every language that has the ability to push/pop elements from an array will also have the ability to unshift/shift (sometimes called push_front/pop_front) elements, you should never have to implement these yourself.


    As pointed out in the comments, if you want to avoid mutating your original array, you can use concat, which concatenates two or more arrays together. You can use this to functionally push a single element onto the front or back of an existing array; to do so, you need to turn the new element into a single element array:

    const array = [3, 2, 1]
    
    const newFirstElement = 4
    
    const newArray = [newFirstElement].concat(array) // [ 4, 3, 2, 1 ]
    
    console.log(newArray);

    concat can also append items. The arguments to concat can be of any type; they are implicitly wrapped in a single-element array, if they are not already an array:

    const array = [3, 2, 1]
    
    const newLastElement = 0
    
    // Both of these lines are equivalent:
    const newArray1 = array.concat(newLastElement) // [ 3, 2, 1, 0 ]
    const newArray2 = array.concat([newLastElement]) // [ 3, 2, 1, 0 ]
    
    console.log(newArray1);
    console.log(newArray2);
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form and I need to add some data from database before
I update the html with ajax call. I need to add element as hidden
I have a need to add business reporting for an application I am working
I have a COM interface and need to add an accessor method to it.
i have a string and i need to add a number to it i.e
I have a 2D matrix that I need to add into a 3D matrix,
I have two lists: ask[] timeStamp[] I need to add the information in these
I have an Inventory Database and I need to add a validation rule to
I need to add a custom search engine to Firefox. I have a name
I have a DataGrid and I need to add an Expander control dynamically to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.