in computing, a condition where an expression does not have a correct value

In computing (particularly, in programming), undefined value is a condition where an expression does not have a value semantically correct in the object language, although it is syntactically correct. An undefined value … Wikipedia
🌐
Wikipedia
en.wikipedia.org › wiki › Undefined_value
Undefined value - Wikipedia
1 week ago - In computing (particularly, in programming), undefined value is a condition where an expression does not have a value semantically correct in the object language, although it is syntactically correct. An undefined value must not be confused with empty string, Boolean "false" or other "empty" ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › undefined
undefined - JavaScript | MDN
In all non-legacy browsers, undefined is a non-configurable, non-writable property. Even when this is not the case, avoid overriding it. A variable that has not been assigned a value is of type undefined. A method or statement also returns undefined if the variable that is being evaluated does ...
Top answer
1 of 16
3234

If you are interested in finding out whether a variable has been declared regardless of its value, then using the in operator is the safest way to go. Consider this example:

// global scope
var theFu; // theFu has been declared, but its value is undefined
typeof theFu; // "undefined"

But this may not be the intended result for some cases, since the variable or property was declared but just not initialized. Use the in operator for a more robust check.

"theFu" in window; // true
"theFoo" in window; // false

If you are interested in knowing whether the variable hasn't been declared or has the value undefined, then use the typeof operator, which is guaranteed to return a string:

if (typeof myVar !== 'undefined')

Direct comparisons against undefined are troublesome as undefined can be overwritten.

window.undefined = "foo";
"foo" == undefined // true

As @CMS pointed out, this has been patched in ECMAScript 5th ed., and undefined is non-writable.

if (window.myVar) will also include these falsy values, so it's not very robust:

false
0
""
NaN
null
undefined

Thanks to @CMS for pointing out that your third case - if (myVariable) can also throw an error in two cases. The first is when the variable hasn't been defined which throws a ReferenceError.

// abc was never declared.
if (abc) {
    // ReferenceError: abc is not defined
} 

The other case is when the variable has been defined, but has a getter function which throws an error when invoked. For example,

// or it's a property that can throw an error
Object.defineProperty(window, "myVariable", { 
    get: function() { throw new Error("W00t?"); }, 
    set: undefined 
});
if (myVariable) {
    // Error: W00t?
}
2 of 16
1586

I personally use

myVar === undefined

Warning: Please note that === is used over == and that myVar has been previously declared (not defined).


I do not like typeof myVar === "undefined". I think it is long winded and unnecessary. (I can get the same done in less code.)

Now some people will keel over in pain when they read this, screaming: "Wait! WAAITTT!!! undefined can be redefined!"

Cool. I know this. Then again, most variables in Javascript can be redefined. Should you never use any built-in identifier that can be redefined?

If you follow this rule, good for you: you aren't a hypocrite.

The thing is, in order to do lots of real work in JS, developers need to rely on redefinable identifiers to be what they are. I don't hear people telling me that I shouldn't use setTimeout because someone can

window.setTimeout = function () {
    alert("Got you now!");
};

Bottom line, the "it can be redefined" argument to not use a raw === undefined is bogus.

(If you are still scared of undefined being redefined, why are you blindly integrating untested library code into your code base? Or even simpler: a linting tool.)


Also, like the typeof approach, this technique can "detect" undeclared variables:

if (window.someVar === undefined) {
    doSomething();
}

But both these techniques leak in their abstraction. I urge you not to use this or even

if (typeof myVar !== "undefined") {
    doSomething();
}

Consider:

var iAmUndefined;

To catch whether or not that variable is declared or not, you may need to resort to the in operator. (In many cases, you can simply read the code O_o).

if ("myVar" in window) {
    doSomething();
}

But wait! There's more! What if some prototype chain magic is happening…? Now even the superior in operator does not suffice. (Okay, I'm done here about this part except to say that for 99% of the time, === undefined (and ****cough**** typeof) works just fine. If you really care, you can read about this subject on its own.)

🌐
W3Schools
w3schools.com › jsref › jsref_undefined.asp
JavaScript undefined Property
The undefined property indicates that a variable has not been assigned a value, or not declared at all.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-check-for-undefined-value-in-javascript
How to check for "undefined" value in JavaScript ? - GeeksforGeeks
July 23, 2025 - In JavaScript, undefined is a primitive value that represents the absence of a value or the uninitialized state of a variable. It's typically used to denote the absence of a meaningful value, such as when a variable has been declared but not ...
🌐
Rosetta Code
rosettacode.org › wiki › Undefined_values
Undefined values - Rosetta Code
August 28, 2025 - There is no "undefined" value as every instruction that is capable of altering the contents of a register will set the value of that register equal to some number between 0 and 0xFFFFFFFF.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › undefined-in-javascript
Undefined in JavaScript - GeeksforGeeks
March 13, 2024 - Undefined is a type of Data type in JavaScript. It is a primitive value undefined, when a variable is declared and not initialized or not assigned with any value.
🌐
Fiveable
fiveable.me › all key terms › intermediate algebra › undefined values
Undefined Values - (Intermediate Algebra) - Vocab, Definition, Explanations | Fiveable
Undefined values refer to the concept in mathematics where a variable or expression cannot be assigned a specific numerical value. This often occurs when the denominator of a rational expression is zero, or when attempting to solve a rational equation that results in an invalid solution.
🌐
Musing Mortoray
mortoray.com › home › the many faces of undefined in javascript
The many faces of undefined in JavaScript - Musing Mortoray
June 26, 2024 - It can, however, be a null value. optional_value is also nullable, but it doesn’t need to exist at all. undefined lets us distinguish between a specified null value, and the value not existing at all.
🌐
GitHub
github.com › porsager › postgres › discussions › 314
Error: Undefined values are not allowed - Undefined should map to Null · porsager/postgres · Discussion #314
Postgres.js won't accept undefined as values in tagged template queries since it becomes ambiguous what to do with the value. If you want to set something to null, use null explicitly. Undefine...
Author   porsager
🌐
Palantir
palantir.com › docs › foundry › functions › undefined-values
Functions • TypeScript v1 • Handle undefined values • Palantir
By checking that both the firstName and lastName fields are defined, the TypeScript compiler knows that the final line with the return statement can compile correctly. The benefit of this approach is that type checking is more explicit, and in the case where undefined values are present, you can throw a more explicit error about what went wrong.
🌐
web.dev
web.dev › learn › javascript › data-types › null-undefined
null and undefined | web.dev
Although undefined and null have some functional overlap, they have different purposes. In the strictest sense, null represents a value intentionally defined as "blank," and undefined represents a lack of any assigned value.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Glossary › Undefined
Undefined - Glossary | MDN
undefined is a primitive value automatically assigned to variables that have just been declared, or to formal arguments for which there are no actual arguments.
🌐
Wikipedia
en.wikipedia.org › wiki › Undefined_(mathematics)
Undefined (mathematics) - Wikipedia
November 4, 2025 - The term undefined should be contrasted with the term indeterminate. In the first case, undefined generally indicates that a value or property can have no meaningful definition. In the second case, indeterminate generally indicates that a value or property can have many meaningful definitions.
🌐
Medium
medium.com › front-end-weekly › beginners-guide-dealing-with-undefined-in-javascript-d98ac7e413db
Beginner’s Guide: Dealing with Undefined in JavaScript | by Brandon Evans | Frontend Weekly | Medium
June 8, 2023 - If person is undefined, the result will be undefined without throwing an error. Similarly, accessing the non-existing profession property returns undefined instead of throwing an error. When defining functions, you can set default values for function parameters.