typeof is safer as it allows the identifier to never have been declared before:

if(typeof neverDeclared === "undefined") // no errors

if(neverDeclared === null) // throws ReferenceError: neverDeclared is not defined
Answer from seanmonstar on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › undefined
undefined - JavaScript | MDN
Note: The strict equality operator (as opposed to the standard equality operator) must be used here, because x == undefined also checks whether x is null, while strict equality doesn't. This is because null is not equivalent to undefined. See Equality comparison and sameness for details. ... One reason to use typeof is that it does not throw an error if the variable has not been declared.
Discussions

"undefined" === typeof(var)" vs "typeof(var) === "undefined"
For most, A == B is the logically the same as B == A. However, I’ve seen code written both ways. I can’t believe they are identical in terms of computation, ordering/short circuiting, etc. Can someone please explain the technical difference between the two? More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
1
0
August 27, 2019
`typeof window === "undefined"` not simplified when `--define:window=undefined`
In webpack's case, I think people usually make it "typeof window": "undefined" so that they theoretically could still have a window variable in the code somewhere. More on github.com
🌐 github.com
2
June 29, 2021
If (typeof s== "undefined") vs if (s == undefined)... both do exactly the same thing?
I do see if (typeof s == “undefined”) { … } and sometimes if (s == undefined) { … } do both do exactly the same thing? if they are the same, then maybe the second one is more preferred, since it is shorter and no need to quote the word “undefined”. I am doing this to check for optional ... More on sitepoint.com
🌐 sitepoint.com
0
September 24, 2008
Check for undefined variable isn't working
There's a difference between a variable that has a value of undefined, and a variable that has never been declared at all. If your code has no declaration for zebra, such as let zebra;, then there is literally nothing for the Javascript engine to reference when you ask it to. This causes an a ReferenceError. edit: as u/shgysk8zer0 pointed out, you can use the typeof operator for your use case. It is unique as it will not throw an error when referencing an undeclared variable. I would warn that this is probably an uncommon use case - ideally your code should be written in a way that variable declarations are known and don't need something like this. More on reddit.com
🌐 r/learnjavascript
8
1
June 10, 2023
People also ask

Can I use typeof on undeclared variables in JavaScript?
Yes, typeof returns "undefined" for undeclared variables without throwing an error.
🌐
builtin.com
builtin.com › software-engineering-perspectives › javascript-array-typeof
JavaScript Typeof for Data Types: Array, Boolean and More | Built In
What does the typeof operator do in JavaScript?
The typeof operator in JavaScript checks the data type of a value and returns a string that details what the type is, like "string", "number" or "object".
🌐
builtin.com
builtin.com › software-engineering-perspectives › javascript-array-typeof
JavaScript Typeof for Data Types: Array, Boolean and More | Built In
How can I check if a variable is an array in JavaScript?
Use Array.isArray() or Object.prototype.toString.call() to accurately check if a JavaScript variable is an array data type, since typeof returns "object" for arrays.
🌐
builtin.com
builtin.com › software-engineering-perspectives › javascript-array-typeof
JavaScript Typeof for Data Types: Array, Boolean and More | Built In
🌐
Reddit
reddit.com › r/javascript › [askjs] why does typeof undefined return "undefined" — and is there any actual use case where this is helpful?
r/javascript on Reddit: [AskJS] Why does typeof undefined return "undefined" — and is there any actual use case where this is helpful?
April 16, 2025 -

I’ve seen this behavior for years, but I’m trying to understand if there’s a real-world use case where typeof undefined === "undefined" is practically useful, versus just a quirky historical thing.

For example, in older codebases, I see checks like if (typeof myVar === "undefined"), but nowadays with let, const, and even nullish coalescing, this feels outdated.

So — is there a valid modern use case for typeof undefined comparisons, or is it mostly just something legacy that we put up with?

🌐
W3Schools
w3schools.com › js › js_typeof.asp
W3Schools.com
The typeof of a variable with no value is undefined.
🌐
Sentry
sentry.io › sentry answers › javascript › how can i check for "undefined" in javascript?
How can I Check for "undefined" in JavaScript? | Sentry
December 15, 2022 - A reserved word is a keyword that ... import, const, and return. You can use undefined as a variable name, as long as the variable is not in the global scope. As can be seen in the example code below, you can make the typeof undefined equal to string:...
Find elsewhere
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › typeof
typeof - JavaScript | MDN
typeof is generally always guaranteed to return a string for any operand it is supplied with. Even with undeclared identifiers, typeof will return "undefined" instead of throwing an error.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › variable-undefined-vs-typeof-variable-undefined-in-javascript
variable === undefined vs. typeof variable === “undefined” in JavaScript - GeeksforGeeks
July 12, 2025 - Check by Value (Strict equality Operator): Here you will get whether the variable is assigned a value or not if the variable is not assigned a value it will display undefined. Check the type (Typeof operator): Here you will get what type of variable was that if there is no variable assigned then it will display "undefined".
🌐
freeCodeCamp
freecodecamp.org › news › javascript-check-if-undefined-how-to-test-for-undefined-in-js
JavaScript Check if Undefined – How to Test for Undefined in JS
November 7, 2024 - We can also use the type of the variable to check if it’s undefined. Luckily for us undefined is a datatype for an undefined value as you can see below: ‌ · let name; console.log(typeof name); // "undefined" With this we can now use the datatype to check undefined for all types of data as we saw above.
🌐
2ality
2ality.com › 2013 › 04 › check-undefined
Checking for undefined: === versus typeof versus falsiness
April 19, 2013 - It has two advantages: First, it is safe with regard to a changed undefined (not that important under ECMAScript 5). Second, it also works for unknown variables: > typeof iDontKnowThisVariable === 'undefined' true > iDontKnowThisVariable === undefined ReferenceError: iDontKnowThisVariable is not defined
🌐
Built In
builtin.com › software-engineering-perspectives › javascript-array-typeof
JavaScript Typeof for Data Types: Array, Boolean and More | Built In
If typeof says a value is "undefined", then it’s a safe bet to assume that value is actually undefined, meaning it was not declared, declared but never assigned a value or declared and assigned the value of undefined.
Published   April 11, 2025
🌐
TypeScript
typescriptlang.org › docs › handbook › 2 › everyday-types.html
TypeScript: Documentation - Everyday Types
With strictNullChecks off, values that might be null or undefined can still be accessed normally, and the values null and undefined can be assigned to a property of any type. This is similar to how languages without null checks (e.g. C#, Java) behave.
🌐
Sentry
sentry.io › sentry answers › javascript › undefined versus null in javascript
Undefined versus null in JavaScript | Sentry
October 21, 2022 - When a variable has not been declared or assigned, it will also be of type undefined and throw a ReferenceError when accessed. Therefore, typeof will produce the same result for declared but undefined variables and undeclared variables.
🌐
Flexiple
flexiple.com › javascript › undefined-vs-null-javascript
Undefined vs Null - Javascript - Flexiple
March 10, 2022 - The data type of undefined is undefined whereas that of null is object. We can find the datatypes of both undefined and null using the typeof operator.
🌐
DEV Community
dev.to › kais_blog › how-to-check-for-undefined-in-javascript-typescript-3men
How to Check For Undefined in JavaScript / TypeScript - DEV Community
January 2, 2021 - The correct way to check if something is undefined in JavaScript / TypeScript: Use the typeof operator!
🌐
Mastering JS
masteringjs.io › tutorials › fundamentals › typeof-undefined
How to Determine if a Variable is undefined - Mastering JS
May 21, 2021 - To check if a variable is undefined, you should use the typeof operator. When used on an undefined variable, it will return 'undefined'. If you use that in conjunction with the === operator, you can successfully check for those specific values.
🌐
GitHub
github.com › evanw › esbuild › issues › 1407
`typeof window === "undefined"` not simplified when `--define:window=undefined` · Issue #1407 · evanw/esbuild
June 29, 2021 - It works as expected if you replace window with undefined. ... if (typeof undefined !== "undefined") { console.log("this code should not exist"); } else { console.log("this code should exist"); }
Published   Jun 29, 2021
🌐
web.dev
web.dev › learn › javascript › data-types › null-undefined
null and undefined | web.dev
JavaScript has multiple ways of indicating the absence of a value. This page describes the two most common ways: the null and undefined data types. The null keyword represents an intentionally defined absence of value. null is a primitive, although the typeof operator returns that null is an object.
🌐
BrowserStack
browserstack.com › home › guide › how to check if a variable is undefined in javascript
How to Check if a Variable is Undefined in JavaScript | BrowserStack
February 18, 2025 - undefined: This is the default value of uninitialized variables. It is automatically assigned when a variable is declared without a value. It is also the return value of a function that does not explicitly return anything. null: This is an explicit ...
🌐
SitePoint
sitepoint.com › javascript
If (typeof s== "undefined") vs if (s == undefined)... both do exactly the same thing?
September 24, 2008 - I do see if (typeof s == “undefined”) { … } and sometimes if (s == undefined) { … } do both do exactly the same thing? if they are the same, then maybe the second one is more preferred, since it is shorter and no need to quote the word “undefined”. I am doing this to check for optional parameters for a function… so that if a param is undefined, then it will use some default value for it.