Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

window is the global object, so properties on it can be accessed as if they were standalone variables. So referencing top on the top level is the same as referencing window.top . So the question boils down to the difference is between

window.top
window.top.window

window.top gives you a window object (it may be the same as window, or it may be an outer window, if you're in an iframe), and a window's window property is a reference to the same window object (it's a weird self-reference), so those two references are exactly the same as well. You can count on

window.top === window.top.window

to always be true.

Very informative, thanks! So the second part of my question would be, why? Why can window reference itself? As it is also possible to do window.window and window.window.window.window and so on. – lukehillonline Dec 6, 2019 at 12:28 MDN says: The point of having the window property refer to the object itself, was likely to make it easy to refer to the global object. Otherwise, you'd have to do a manual var window = this; assignment at the top of your script. Yes, you can chain windows infinitely, just like obj = {}; obj.obj = obj; obj.obj.obj.obj.obj === obj will give true – CertainPerformance Dec 6, 2019 at 12:31

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.