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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:10:26+00:00 2026-05-26T04:10:26+00:00

I am quite new to JavaScript and just started some more serious development in

  • 0

I am quite new to JavaScript and just started some more serious development in JavaScript. I had a lot of fun implementing the Module pattern. One thing that really drove me crazy was the behavior of the ‘return’ statement. It is a big difference if you write

Test = ( function()
{
    var message = "Hello World!";

    return
    {
        // Does not work
        printTest: function() { window.alert(message); }
    };
}());

or

Test = ( function()
{
    var message = "Hello World!";

    return {
        // Works well
        printTest: function() { window.alert(message); }
    };
}());

Note the curly brace after the ‘return’ statement.

Is that a typical stupid rookie error and is well documented somewhere?

Firebug was not able to give a hint. IE9 and Chrome did report some obscure syntax error at a later location in the code: the opening brace after the ‘function‘ statement in “printTest: function()“.

Any comments on this? Are there more such pitfalls in JavaScript?

  • 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-26T04:10:27+00:00Added an answer on May 26, 2026 at 4:10 am

    If you put your brackets to the next line, the interpreter assumes that there is a semi-colon.

    So your return statement will be interpreted as:

    return; 
    {
       printTest: function() { window.alert(message); };
    }
    

    If I remember well, i’ve red about this problem in JavaScript: The Good Parts

    A.3. Semicolon Insertion

    JavaScript has a mechanism that tries to correct faulty programs by automatically inserting semicolons. Do
    not depend on this. It can mask more serious errors.
    It sometimes inserts semicolons in places where they are not welcome. Consider the consequences of
    semicolon insertion on the return statement. If a return statement returns a value, that value expression
    must begin on the same line as the return:

    return
    {
       status: true
    };
    

    This appears to return an object containing a status member. Unfortunately, semicolon insertion turns it
    into a statement that returns undefined. There is no warning that semicolon insertion caused the
    misinterpretation of the program. The problem can be avoided if the { is placed at the end of the previous line
    and not at the beginning of the next line:

    return {
       status: true
    };
    

    “JavaScript: The Good Parts by Douglas Crockford. Copyright 2008 Yahoo! Inc.,
    978-0-596-51774-8.”

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

Sidebar

Related Questions

I'm quite new to Javascript and I was just reading following article. you can
Hi I am quite new to javascript. So I just tried to create a
I'm quite new to javascript but have undertaken a task to get better aquainted
Dear experts, I am quite new to javascript and I often see coders use
Yo. I'm really quite new to this whole JavaScript business, not to mention AJAX,
Hi I am quite new to php but i have been following some tutorials
i'm quite new to Dojo, but i am trying to convert an existing javascript
Being quite new at JavaScript, I'm assuming the answer to my question is extremely
I'm quite new to javascript programming and got stuck in this problem: I have
First, let me apologize, I'm new at web development, so I'm not quite sure

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.