The variable exists, but it's like it doesn't? Let's figure out checks in JavaScript
\n\nHave you ever written a script, run it, and suddenly — bam! — the browser console turns red with the error Uncaught ReferenceError: myVar is not defined? Sounds familiar. Or the opposite: a variable is declared, but you're not sure if a value has been assigned to it. In JavaScript, these two concepts ("exists" and "initialized") are often confused even by experienced developers.
In this guide, we will once and for all figure out how to properly check if a variable exists in JavaScript, how "defined" differs from "initialized", and how to avoid 90% of errors related to undefined and ReferenceError. Let's go!
The gist: what does "a variable exists" mean?
\n\nIn JavaScript, a variable has three states:
\n\n- \n
- Undeclared — the variable does not exist in memory at all. Trying to access it will throw a
ReferenceError. \n - Declared, but not initialized — we wrote
let x;orvar y;, but didn't assign a value. The variable exists, its type isundefined. \n - Declared and initialized — the variable has a specific value (number, string, object, etc.). \n
Most questions on Stack Overflow (and there are thousands of them) boil down to one thing: "How do I know that a variable won't throw an error when accessed?" The answer is — use typeof. But there are nuances. Let's break it down.
Method #1. The typeof operator — your best friend
\n\nThe most reliable and safe way to check if a variable is defined in JavaScript is the typeof operator. Its main advantage: it never throws an error, even if the variable is not declared.
Example for an undeclared variable
\n\n// Variable myVar is not declared\nif (typeof myVar !== 'undefined') {\n console.log('The variable exists!');\n} else {\n console.log('The variable does NOT exist or its value is undefined');\n}\n// Output: "The variable does NOT exist or its value is undefined"\n\nNote: typeof returns the string 'undefined' both for an undeclared variable and for a declared but uninitialized one (let x;). This is both good and bad. Good — because we don't catch an error. Bad — because we don't distinguish between these two cases.
Example for a declared but empty variable
\n\nlet myVar; // declared but not initialized\nconsole.log(typeof myVar); // "undefined" — also undefined!\n\nif (typeof myVar !== 'undefined') {\n console.log('The variable exists and has a value');\n} else {\n console.log('The variable is undefined (either not declared or not initialized)');\n}\n\nConclusion: typeof is the ideal solution if you just need to avoid ReferenceError. If you need to distinguish between "not declared" and "empty" — read on.
Method #2. Checking via window (global context)
\n\nIf you are working in a browser and the variable is declared globally (via var or implicitly), it becomes a property of the window object. This opens up additional possibilities for checking if a variable exists.
Using the in operator
\n\n// Global variable\nvar globalVar = 42;\n\nconsole.log('globalVar' in window); // true\nconsole.log('nonExistentVar' in window); // false\n\nThis method only works for global variables declared with var or assigned without a keyword. let and const do not create properties of the window object (they live in block scope).