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

  • Home
  • SEARCH
  • 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 9011849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:51:00+00:00 2026-06-16T02:51:00+00:00

When I try to push elements into a javascript array, it doesn’t work. This

  • 0

When I try to push elements into a javascript array, it doesn’t work.
This is the simplest sample I can come up with.
Why doesn’t this work?

    <!DOCTYPE html>
    <html>
    <body>

    <script>
    var i;
    var mycars = new Array();

    for(i=1;i<=10;i++){
      mycars.push[ i.toString()+"-" ];
    }

    alert(mycars.join(""));

    </script>

    </body>
    </html>
  • 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-06-16T02:51:01+00:00Added an answer on June 16, 2026 at 2:51 am

    push is a function. You call functions with (), not []:

    mycars.push( i.toString()+"-" );
    // here ---^     and here ----^
    

    Where you probably got confused is that you can add to an array without using push, just by assigning to an array element, even if that element doesn’t exist yet. So for instance, your loop could look like this:

    for(i=1;i<=10;i++){
      mycars[mycars.length] = i.toString()+"-";
    }
    

    There, because I’m referring to an array element (mycars[mycars.length]), not calling a function, I use [].

    Side note: Rather than var mycars = new Array();, just write var mycars = [];. It does the same thing but is more concise and not prone to side effects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Question: Why can't I push elements into a 2d array that's inside of a
I'm trying to populate a multidimensional array (array with array elements) via Javascript or
I'll try to explain this as best as i can, but i appologize if
I am going to try to make this question as clear as I can.
I have this code, that takes a string and splits it into an array:
I am tring to push an array of strings to a vector, like this:
What does it mean when you try to push to a Mercurial repository on
What would possibly cause a 'git push' to try and commit to two branches?
Try this code - import java.io.StringReader; public class StringReaderTest { public static void main(String[]
Try this piece of code - public class WhitespaceTest { public static void main(String[]

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.