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 8992881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:03:44+00:00 2026-06-15T23:03:44+00:00

I have a bunch of javascript functions, that trigger upon button clicks. So assuming

  • 0

I have a bunch of javascript functions, that trigger upon button clicks. So assuming we have two button s that trigger two methods such as:

 var universal = false;
    var collection = Array(); // assume it has 5 data elements( 0...4) upon page load

    function next()
    {
      if(universal)
         addToArray();

       // do whatever else
    }

    function addToArray()
    {

        console.log(collection);
       // perform some DOM calls,that simply Hide/change position of elements
       var newElement = 'some info';   
       collection.push(newElement);
    }

In the above addToArray() function, it is called upon when ‘next’ button is clicked and the universal variable is set to true. At this point we enter, addToArray() method and a console.log is invoked to check out ‘collection’.

This, at any given point, should be not more than 5 elements since addToArray will eventually add an element to collection but not right at the start. However, the console.log shows that collection has the new element added, which was actually supposed to be added during the addToArray method() not right off the bat.

I know this sounds really goofy, but this is what I am getting. I am trying to understand if asynchronous behavior has anything to do with it.

  • 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-15T23:03:45+00:00Added an answer on June 15, 2026 at 11:03 pm

    If you’re using Chrome, the reason is that console.log logs a live version of the object that updates in the console window when you change it later.

    If you change

    console.log(collection);
    

    …to

    console.log(collection.join(","));
    

    …or probably more usefully

    console.log(JSON.stringify(collection));
    

    …you’ll see a point-in-time version of the array.

    The behavior of console.log is actually quite tricky, although consistent once you get your head ’round it. For instance, consider this code:

    var a = [1, 2, 3];
    
    console.log("first");
    console.log(a);
    console.log("second");
    a[0] = '11';
    a = ['a', 'b', 'c'];
    console.log("third");
    

    Live Copy | Source

    If you have the console open when you run that, you see:

    first
    [1, 2, 3]
    second
    third

    …and no amount of clicking on that [1, 2, 3] will show you the "11" the code put at index 0, much less the new array we put in a.

    But if you change the

    var a = [1, 2, 3];
    

    …to

    var a = [[1, 1], 2, 3];
    

    Live Copy | Source

    …you see this:

    first
    >[Array[2], 2, 3]
    second
    third

    …and clicking the arrow next to the array changes it to this:

    first
    v[Array[2], 2, 3]
        0: "11"
        1: 2
        2: 3
        length: 3
        __proto__: Array[0]
    second
    third

    …which looks a bit non-sensical.

    But it does make sense, you have to think in terms of the console having a reference to the thing you asked it to output. It outputs a line for the thing, and that line is static. But because it saw that the object graph went deeper, Chrome put an arrow next to the array so you could expand it. When you click the array, the new lines added to the output are generated then, by looking at the now current version of the object, which has obviously changed since the first line was output. Tricky, eh?

    In the comments, epascarello mentions using console.dir instead, but that just makes it even more reliably “live,” rather than being a point-in-time record.

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

Sidebar

Related Questions

I have a bunch of javascript functions which depend on some server-side constants (such
I have a bunch of Javascript functions that look like the following: function generateBusinessImage
I'm working with animation in JavaScript, and I have a bunch of functions you
I have a bunch of functions (methods of a class actually) and I'm writing
I have a bunch of controls: When a user clicks the Generate button, a
I have an express app that has a bunch of static javascript files that
I have a bunch of functions that I fire up based on the width
I have a bunch of console.log() calls in my JavaScript. Should I comment them
I'm a little new to javascript. I have a bunch of checkboxes for an
I have a bunch of html files that come with my app and are

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.