JavaScript Programmers Can’t Stand on top

The catch of an innocent identifier as global variable

On the top
On the top

Please, before you throw anything at me, take a look at the code below.

You can’t stand on top

Testing “top” as global variable
Testing “top” as global variable

Chrome: ErrorUncaught TypeError: Cannot assign to read only property ‘top’ of object ‘#<Window>’ …

Firefox: Uncaught TypeError: setting getter-only property “top” <anonymous> …

Standing on bottom is OK

Testing “bottom” as global variable
Testing “bottom” as global variable

Notes

You may use “top” to name a local variable (declared inside a function), but not to name a global variable (declared outside a function).

But its “inferior” friend, “bottom” you can use as local and as global variable name.

This article was made using

  • Chrome version 85.0
  • Firefox version 79.0

I had a bad time in the past because Chrome (old version)

  1. allowed me to declare “top” as global variable
  2. silently rejected the assignment to global variable “top”

A bad time…

--

--

Responses (1)