• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

220
Views
Why can you redefine globalThis and global but not window?

let globalThis = undefined gives me no error in browsers
let [global, globalThis] = [undefined, undefined] gives me no error in node
However let window = undefined throws SyntaxError

almost 3 years ago · Juan Pablo Isaza
1 answers
Answer question

0

window must be present for the page(frame) to work. Since window is declared in a global execution context, you can't redeclare it with let or const.

Although, it's possible to call the following code: var window = undefined; or window = undefined;, but it won't do anything to the window object. That's because the setter on the window object is undefined and configurable descriptor is set to false, disallowing changes and deletion of the window property. You can see that if you that with the following code:

console.log(Object.getOwnPropertyDescriptor(this, 'window'))

You are still able to declare your own window variable inside any other lexical context, other than the global one:

const test = () => {
  const window = 123;
  console.log('in test fn:', window);
}
test()
console.log('in global:', window)

But why can I change the globalThis then? You can change it because globalThis is the property of window. And doesn't seem to have an empty setter and configurable descriptor is set to true, allowing changes to this property. All that means, you can do anything you want with globalThis property :)

console.log(window.hasOwnProperty('globalThis'));
console.log(Object.getOwnPropertyDescriptor(window, 'globalThis'));

almost 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error