x === undefined and typeof x === 'undefined' differs in exactly one case: when the expression being checked is an undeclared variable console.log(typeof undeclared === 'undefined') // true console.log(undeclared === undefined) // ReferenceError In any other case the two is functionally equivalent Answer from TwiNighty on reddit.com
🌐
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?

🌐
REACT NATIVE FOR YOU
reactnativeforyou.com › how-to-check-undefined-property-in-react-native
How to Check undefined Property in React Native – REACT NATIVE FOR YOU
May 12, 2019 - The typeof returns string ‘undefined’ when the value is not defined. Hence you can check the value undefined or not with that string. ... const book = { title: 'ReactNativeForYou' }; console.log(typeof book.author); //undefined if (typeof book.author === 'undefined'){ //Do your things }
🌐
Sentry
sentry.io › sentry answers › javascript › how can i check for "undefined" in javascript?
How can I Check for "undefined" in JavaScript? | Sentry
function myFunction() { const undefined = "string value"; console.log(undefined); // string value console.log(typeof undefined); // string } myFunction();
🌐
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
🌐
REACT NATIVE FOR YOU
reactnativeforyou.com › how-to-check-a-variable-is-undefined-in-react-native
How to Check a Variable is Undefined in React Native – REACT NATIVE FOR YOU
June 21, 2019 - Typeof returns data type, that is whether the variable is “undefined”, “object”, “boolean”, “string”, “number”, “function” etc.
Find elsewhere
🌐
Reddit
reddit.com › r/typescript › undefined vs null
r/typescript on Reddit: Undefined vs null
February 27, 2023 -

Since switching to TypeScript I have been using a lot of optional properties, for example:

type store = {
  currentUserId?: string
}

function logout () {
  store.currentUserId = undefined
}

However my coworkers and I have been discussing whether null is a more appropriate type instead of undefined, like this:

type store = {
  currentUserId: string | null
}

function logout () {
  store.currentUserId = null
}

It seems like the use of undefined in TypeScript differs slightly from in Javascript.

Do you guys/girls use undefined or null more often? And, which of the examples above do you think is better?

🌐
2ality
2ality.com › 2013 › 04 › check-undefined
Checking for undefined: === versus typeof versus falsiness
Technique 2: compare with void 0. The void operator [2] evaluates its operand, ignores the result and returns undefined. That means that void 0 will always evaluate to undefined. ... if (typeof x === 'undefined') ... This is more verbose and can be slower (though many engines optimize).
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › variable-undefined-vs-typeof-variable-undefined-in-javascript
variable === undefined vs. typeof variable === “undefined” ...
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"....
🌐
W3Schools
w3schools.com › jsref › jsref_undefined.asp
JavaScript undefined Property
let x; if (x === undefined) { text = "x is undefined"; } else { text = "x is defined"; } Try it Yourself » · let x; if (typeof x === "undefined") { text = "x is undefined"; } else { text = "x is defined"; } Try it Yourself »
🌐
DPS Computing
dpscomputing.com › blog › 2022 › 04 › 20 › typeerror-undefined-is-not-an-object-react
TypeError: undefined is not an object – React
April 20, 2022 - If previousCaller being undefined is a valid scenario, then you can just leave it at the code above – there’s no point clogging logs with unnecessary information! If it should always be defined, you can use something like this: if(!typeOf(error.previousCaller) == 'undefined'){ //this will now only run if error.previousCaller is defined console.error(error.previousCaller.name) }else{ //previousCaller is undefined - log away for analysis console.error('previousCaller in function x is undefined at line x') }
🌐
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 - In the actual sense, this works like direct comparison (which we saw before). But we would replace undefined with void(0) or void 0 as seen below: if(typeof user.hobby === void 0){} if(typeof scores[10] === void 0){} if(typeof name === void 0){}
Top answer
1 of 1
3

Javascript doesn't necessarily have a compilation phase. Granted, most implementations do compile, but there isn't a single phase in which this happens. The access to the window variable does not throw until it is executed.

When using React, the normal way to run code only on the client is to put it inside componentDidMount.

In any case I would advise to not attempt to create fake versions of client-side APIs on the server to prevent breakage. Instead, make sure your client-side code only ever runs on the client.

You could wrap it in a function and call it only from componentDidMount or you could test the environment.

function clientInitialize() {
  // start your client side app here
  // create client-side store here
}

function serverInitialize() {
  // create server-side store here
}

if (typeof window === 'object') {
  clientInitialize();
} else {
  serverInitialize();
}

I should note that server side code should preferably never be shipped to the client for reasons of security and code size. It is better to initialize the store from code that won't be available on the client at all.

There is no one way to do these things and there are pros and cons for each method.

Dan Abramov, the creator of Redux, I believe is a big proponent of knowing the reasons for every piece of code you have, to understand the libraries you use, and to prefer a method you understand over an accepted standard method. Redux itself is heavily based on these ideas which is why it doesn't prescribe much. It's also why there is such a teeming add-on community around Redux.

There really is no need to force all code to be compatible with both the server and the client. Just separate the parts that necessarily must be separate.

🌐
TypeOfNaN
typeofnan.dev › how-to-fix-undefined-this-state-in-react-class-components
How To Fix Undefined 'this.state' in React Class Components | TypeOfNaN
When we click the Increment or Decrement button, we’ll get an error: Cannot read property 'count' of undefined. Our console will show us that indeed it appears this.state is undefined: ... React components using ES6 classes do not autobind this to non-React.Component methods.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-check-for-undefined-value-in-javascript
How to check for "undefined" value in JavaScript ? - GeeksforGeeks
July 23, 2025 - There are a few ways to check for 'undefined'. ... Here, 'typeof' operator returns the string 'undefined' if the variable has not been assigned a value.
🌐
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.