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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:37:56+00:00 2026-05-18T02:37:56+00:00

ECMAScript is pretty straightforward about var . If you don’t use var inside a

  • 0

ECMAScript is pretty straightforward about var. If you don’t use var inside a function to declare a variable you assign to you assign to the global scope. This happens because of the way chain scoping works. The executing environment looks for the identifier in the local scope and then moves up until it reaches the global scope. If hasn’t found a declaration for the the identifier and it’s not identified as an argument the variable is created in the global scope.

For example local scope:

var car = 'Blue';

function change_color () {
  var car = 'Red';
} 
change_color();
console.log(car); //logs 'Blue' as car is in the local scope of the function.

When car is not found in the local scope:

var car = 'Blue';
function change_color () {
  car = 'Red';
}
change_color();
console.log(car); 
//logs 'Red' as car is not in the local scope and the global variable is used.

Now apparently there’s an exception to this rule that I wasn’t aware of and don’t understand (notice the function name):

var car = 'Blue';
(function car () {
 car = 'Red';
})();
console.log(car); //Logs 'Blue'????

Can anyone explain this please? I don’t see where this is explained in the ECMASpec. Tested in Chrome 8 and Firefox 3.6

  • 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-18T02:37:57+00:00Added an answer on May 18, 2026 at 2:37 am

    A named function expression (unlike a function declaration) creates a name which is only available within the scope of the function.

    An expression (not declaration) of the form (function foo() { ... }) creates an identifier named foo which only exists inside the function.
    Therefore, when you assign to foo inside the function, you’re assigning to this local identifier.

    If you change your function to a function declaration, it will work as you expect:

    var car = 'Blue';
    function car () {
        car = 'Red';
    }
    car();
    console.log(car); //Logs 'Red'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

From http://www.jibbering.com/faq/faq_notes/closures.html : Note: ECMAScript defines an internal [[prototype]] property of the internal Object
Have you tried using straight javascript (rather straight ECMAscript 3 standard) in Flex applications,
Google is failing me on this one. Let's say I have some ECMA script

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.