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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:40:23+00:00 2026-05-28T17:40:23+00:00

Every property of the window object is a global variable. This means you can

  • 0

Every property of the window object is a global variable. This means you can do something like this:

window.foo = 42;
alert(foo); //alerts 42
var bar = 3;
alert(window["bar"]); //alerts 3

Because of this, I’ve always wondered what the purpose was of referencing window in code like this

if(window.prompt("Enter Password") === "secret") {
  window.location.hash = "authorized";
  window.open("secretPage.html");
}

when you could omit window and have code that does exactly the same thing:

if(prompt("Enter Password") === "secret") {
  location.hash = "authorized";
  open("secretPage.html");
}

The use also seems inconsistent; I almost never see window.alert but I’ll frequently see window.location.

Do people just like referencing window to make their code more verbose? Is there some good reason for doing this that I don’t know about?

  • 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-28T17:40:24+00:00Added an answer on May 28, 2026 at 5:40 pm

    One situation in which it helps to be explicit is that it will be immediately clear inside a function that you intend to modify a global variable. For example:

    function blah() {
        // a bunch of code preceding...
        test = "aha!";
    }
    

    Did someone forget to declare test with var? Or is this intentional modification of a global? Compare:

    function blah() {
        // a bunch of code preceding...
        window.test = "aha!";
    }
    

    Now it’s immediately clear what is intended. Of course, you know, globals should be avoided in the first place, but you get my point.

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

Sidebar

Related Questions

Every global variable in javascript is a property of 'window' Apparently all local variable
every time i create a FileInfo object and access it's lastaccesstime property, it's always
I'm receiving errors from Gtk that I can't track down. Every time my mouse
When you log LINQ-to-SQL's query output via the Log property on the DataContext object,
Consider this piece of code var crazy = function() { console.log(this); console.log(this.isCrazy); // wrong.
I would like to overrides the datetimepicker object to remove the texte when the
I've created a simple window GUI in Glade 3.6.7 and I am trying to
I'm writing a user control with a dependency property for a search text called
To preface, this is my first attempt at MVVM... I buy it, I'm just
Can anyone explain why the code below, when run, will occasionally result in some

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.