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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:33:58+00:00 2026-05-24T20:33:58+00:00

First Example: In the following example: http://jsfiddle.net/maniator/ScTAW/4/ I have this js: var storage =

  • 0

First Example:

In the following example: http://jsfiddle.net/maniator/ScTAW/4/
I have this js:

var storage = (function () {
    var store = [];
    return {
        "add": function (item) {
            store.push(item);
        },
        "get": function () {
            return store;
        }
    };
}());

storage.add('hi there')
console.log(storage, storage.get(), storage.add('hi there #2'));

And here is what gets printed to the console:

Object
[“hi there”, “hi there #2”] undefined

One would think that the console should only say:

Object
[“hi there”] undefined

becase the second push did not happen until after the value was logged, therefore it should not be displayed.


Second Example:

In the following example: http://jsfiddle.net/maniator/ScTAW/5/

I am using the same storage variable but I log like so:

storage.add('hi there')
console.log(storage, storage.get(), (function() {
    storage.add('hi there #2');
    console.log('TESTING');
})());

What gets printed to the console is:

TESTING
Object
[“hi there”, “hi there #2”] undefined

hmmmm well that is odd now isnt it? One could expect to see:

Object
[“hi there”] undefined
TESTING

Why is this happening? What is going on behind the scenes of the console logging mechanism?

  • 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-24T20:33:59+00:00Added an answer on May 24, 2026 at 8:33 pm

    In most (if not all) imperative programming languages, any arguments passed to a function call have to be evaluated before the function can be called (so called Eager evaluation). Also, they are in general evaluated in order from left to right (for C for instance it’s undefined), however in both examples the order in which the arguments are evaluated does not matter. This should be pretty obvious when looking at what happens in detail:

    As mentioned, before console.log can be called, storage.get() has to be executed first, returning the store array. Then storage.add('hi there #2') will be executed (or the other way round), so its result (in this case undefined, since add does not return anything) can be passed as the third argument to console.log. This means that the once console.log will be called with the arguments (storage, storage.store, undefined), the store array already contains “hi there #2”, hence producing the results you observe.

    In the second example the reasoning is again the same, the function call is just a bit more obscured. On first look it looks there is a function passed as a 3rd argument to the console.log function; but it’s actually a function call (observer the () at the end). So storage.add('hi there #2') will be executed, then console.log('TESTING') and then the undefined result from the anonymous function execution will be again passed to console.log.

    If you did actually pass a function to console.log, it would print that function definition, and not execute anything. So:

    storage.add('hi there')
    console.log(storage, storage.get(), (function() {
        storage.add('hi there #2');
        console.log('TESTING');
    }));
    

    , without the () at the end, results in:

    Object
     ["hi there"] function () {
        storage.add('hi there #2');
        console.log('TESTING');
    }
    

    I hope this makes things a bit clearer.

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

Sidebar

Related Questions

I have the following example setup http://jsfiddle.net/gibble/srBeB/ What I am trying to do, is
I have the following code (which largely follows the first example here: http://www.boost.org/doc/libs/1_42_0/libs/multi_index/doc/examples.html )).
I have the following script: http://jsfiddle.net/oshirowanen/6SUwR/3/ How do I give the drop down section
I have a simple question related to one-line programming. First an example: function test(a)
In one example from http://leepoint.net/notes-java/data/expressions/precedence.html The following expression 1 + 2 - 3 *
I was following this example: http://developer.android.com/resources/tutorials/views/hello-autocomplete.html And I want to know how I can
Why does FindName() return null in the following example? XAML: <Window x:Class=TestDynamicTextBox343.Window1 xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
I have the following http://jsbin.com/useqa4/10 My question is this: In the submenu I hover
Given the following example data: Users +--------------------------------------------------+ | ID | First Name | Last
I have a simple marker annotation for methods (similar to the first example in

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.