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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:55:27+00:00 2026-05-26T21:55:27+00:00

I was just getting ready to get my hands rough with Nodejs, but while

  • 0

I was just getting ready to get my hands rough with Nodejs, but while experimenting and playing a bit on the first example, i found some behaviour which I am finding hard to understand.

var a = function () {
    console.log('print 3');
    return 5000;
    };

setTimeout( function (){
   console.log('print 2');
   }, a()
);

console.log('print 1');

The output to the above code is:

print 3
print 1
print 2

AND a stupid doubt is, while the above code works, this one doesn’t.

setTimeout( function (){
   console.log('print 2');
   }, (function () {console.log('print 3'); return 5000;} )
);
console.log('print 1');

Please explain the above behaviour.

  • 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-26T21:55:28+00:00Added an answer on May 26, 2026 at 9:55 pm

    Formatting the code better should help you understand what is happening.

    var a = function () {
      console.log('print 3');
      return 5000;
    };
    
    setTimeout( function (){
      console.log('print 2'); # Executed after 5000 ms.
      },
    a());                    # Executed first and returns 5000
    
    console.log('print 1');  #Executed second
    

    The following snippet does not work as you never execute the 2nd part of the function.

    setTimeout( function (){
      console.log('print 2');
      }, (function () {            # Never executed.
        console.log('print 3');
        return 5000;
      } )
    );
    
    console.log('print 1'); 
    

    The following will work:

    setTimeout( function (){
      console.log('print 2');
      }, (function () {            
        console.log('print 3');
        return 5000;
      } )();                      #The brackets () will execute the function.
    );
    
    console.log('print 1'); 
    

    As an aside

    The example has nothing to do with node.js and will produce the exact same results in any browser that responds to console.

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

Sidebar

Related Questions

I'm just about getting into WCF ; but from what I've read so far,
I've recently started reading up on Rails and while getting my development environment ready
Just getting my head around Ruby metaprogramming. The mixin/modules always manage to confuse me.
Just getting started with Linq to SQL so forgive the newbie question. I'm trying
Just getting started using MVC in ASP.NET, I'm going to have it so users
Just getting started with C++ here. I am working on OSX with Eclipse CDT.
Just getting started with db2. Quick question about giving it SQL commands from a
Just getting my feet wet with some Fluent NHibernate AutoMap conventions, and ran into
Just getting into SQL stored queries right now... anyway, here's my database schema (simplified
just getting myself familiar with jquery, please consider me as a noob for now.

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.