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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:30:08+00:00 2026-06-01T15:30:08+00:00

In JavaScript in the browser window is the global object, which means every variable

  • 0

In JavaScript in the browser window is the global object, which means every variable defined in the global scope is a child of window. So why do I get this result:

console.log(window.foo); // No error, logs "undefined".
console.log(foo);        // Uncaught ReferenceError: foo is not defined.

Fiddle

Those two lines should be the same, shouldn’t they?

  • 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-06-01T15:30:09+00:00Added an answer on June 1, 2026 at 3:30 pm

    Because with window.foo you are explicitly looking for foo property of window object which is not the case in latter option. In the latter option, if foo isn’t defined, you should as developer be able to know that it isn’t defined and get the clear error warning rather than interpreter setting it to undefined on its own (like first case) which will lead to unexpected results.

    Reference Error:

    Represents an error when a non-existent variable is referenced.
    A ReferenceError is thrown when trying to dereference a variable that has not been declared.

    Take a look at this article for more info:

    • Understanding JavaScript’s ‘undefined’

    Quoting from above article:

    A Reference is considered unresolvable if its base value is undefined. Therefore a property reference is unresolvable if the value before the dot is undefined. The following example would throw a ReferenceError but it doesn’t because TypeError gets there first. This is because the base value of a property is subject to CheckObjectCoercible (ECMA 5 9.10 via 11.2.1) which throws a TypeError when trying to convert Undefined type to an Object.

    Examples:

    var foo;
    foo.bar; //TypeError (base value, foo, is undefined)
    bar.baz; //ReferenceError (bar is unersolvable)
    undefined.foo; //TypeError (base value is undefined)
    

    References which are neither properties or variables are by definition unresolvable and will throw a ReferenceError, So:

    foo; //ReferenceError
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a page which spawns a popup browser window. I have a JavaScript
In javascript on a browser, I can do this to see if an object
Often I see a global object defined in javascript code to act as a
I have found the following javascript code to get browser window size and it
I am unable to resize a browser window using javascript in Firefox 7. This
I'd like some Javascript code to run when the mouse leaves the browser window.
Is there a way with JQUERY or Javascript, to detect anytime the browser window
I want to create a draggable and resizable window in JavaScript for cross browser
How can I check which functions of JavaScript the browser support which I am
How I can capture event, close browser window, in jQuery or javascript ?

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.