Trong browser, var khai báo ở global scope được thêm vào window object vì global execution context gắn với window. let và const ở global scope không thêm vào window, chúng nằm trong script scope riêng biệt. Đây là lý do tránh dùng var ở global scope.
globalThis là cross-environment reference hiện đại: hoạt động trong browser (window), Node.js (global), và Web Workers (self) — thay thế cho code viết typeof window !== "undefined" ? window : global.
In the browser, var declarations at global scope are added to the window object because the global execution context is tied to window. let and const at global scope are not added to window; they exist in a separate script scope. Another reason to avoid var at global scope.
globalThis is the modern cross-environment reference: works in browser (window), Node.js (global), and Web Workers (self) — replacing the old pattern typeof window !== "undefined" ? window : global.