Node.js vs Browser: Understanding Window API

💡 Common Mistake: Using "window" in Node.js Ever run Node.js code and hit: 👉 ReferenceError: window is not defined ? Here’s why 👇 --- 🌍 Browser ✅ "window" exists ✅ "self" exists ✅ "frames" exists They are part of the Window API --- 🟢 Node.js ❌ No "window" ❌ No "self" ❌ No "frames" Because Node.js = server-side runtime, not a browser. --- ✅ What to Use Instead ✔ "global" ✔ "globalThis" (recommended) console.log(globalThis); --- ✅ Safe Check for Browser Code if (typeof window !== "undefined") { // Browser-only logic } --- 🚀 Takeaway: Know your environment. Browser → Window APIs Node.js → Global APIs --- #NodeJS #JavaScript #Backend #WebDevelopment #globalThis

To view or add a comment, sign in

Explore content categories