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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:22:56+00:00 2026-05-24T18:22:56+00:00

See my example code below <script> alert(a); // undefined alert(b); // It is Error,

  • 0

See my example code below

<script>
alert(a); // undefined
alert(b); // It is Error, b is not defined.
var a=1;
b=10;
</script>

When both variable a and b are in global scope, why I am getting error message for b. But there is no error message for variable a ? what is the reason ?

can anyone please explain me ?

  • 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-24T18:22:57+00:00Added an answer on May 24, 2026 at 6:22 pm

    The first alert shows undefined because the var statements are hoisted to the top of the enclosing scope, in other words, var statements and function declarations are made before the actual code is executed, in the parsing stage.

    When your code is executed, is equivalent to:

    var a;    // declared and initialized with `undefined` before the code executes
    alert(a); // undefined
    alert(b); // ReferenceError, b is not declared.
    a=1;
    b=10;
    

    The second alert doesn’t even executes, trying to access b gives you a ReferenceError because you never declared it, and you are trying to access it.

    That’s how the identifier resolution process works in Javascript, if an identifier is not found in all the scope chain, a ReferenceError exception is thrown.

    Also, you should know that assigning an identifier without declaring it first (as b = 10) does not technically declares a variable, even in the global scope, the effect may be similar (and it seems to work), at the end the identifier ends as a property of the global object, for example:

    var a = 1;
    b = 10;
    
    // Similar effect:
    window.a; // 1
    window.b; // 10
    

    But this is just due the fact that the global object is the top-most environment record of the scope chain.

    Another difference between the two above is that the identifier declared with var produces a non-configurable property on the global object (cannot be deleted), e.g.:

    delete window.a; // false
    delete window.b; // true
    

    Also, if you are in the scope of a function, and you make an assignment to an undeclared identifier, it will end up being a property of the global object, just like in the above example, whereas the var statement will create a local variable, for example:

    (function(){
      var a = 1;
      b = 10;
    })();
    
    typeof window.a; // 'undefined', was locally scoped in the above function
    typeof window.b; // 'number', leaked, an unintentional global
    

    I would really discourage make assignments to undeclared identifiers, always use var to declare your variables, moreover, this has been disallowed on ECMAScript 5 Strict Mode, assignments made to undeclared identifiers throw a ReferenceError:

    (function(){'use strict'; b = 10;})(); // throws a ReferenceError
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the example source code below, is it possible for someone to see the
In Apple's documentation and example code, I see lproj folders both named with the
I have the following code example (see below). My problem is that the tooltip
In my example code below, you can see that I have been trying to
Why can't I set $_SERVER['DOCUMENT_ROOT'] as attribute? see example code class foo { private
I see a lot of example code for C# classes that does this: public
I would like to see how this example of existing code would be able
I have an example of some code that I see often in websites that
i'm playing with MEF and in the example i see this code ( i
I want code to run whenever I create a new object. For example, see

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.