undefined in JavaScript: the value of what was never filled in

undefined is the value JavaScript assigns on its own when nothing was supplied. Where it shows up, how to test it, and how to give it a fallback.
3 min read
Believemy logo

It is probably the word you will see most often in a browser console, and rarely for good reasons. An undefined showing up where a first name belongs is almost always a data path leading nowhere. The good news: it is not an error, it is information.


Definition

undefined is the primitive value JavaScript assigns by itself to anything that exists without having received content. Unlike null, nobody writes it: the engine puts it there for you.

Five situations produce it, and knowing them saves a striking amount of debugging time.

JAVASCRIPT
let firstName;
console.log(firstName);         // 1. declared with no value

const client = { name: "Ada" };
console.log(client.age);        // 2. missing property

console.log([10, 20][7]);       // 3. index past the array

function greet(name) { return name; }
console.log(greet());           // 4. argument not passed

function nothing() {}
console.log(nothing());         // 5. function with no return

All five print the same thing. That is exactly why an isolated undefined tells you nothing: you have to step back one level to find out which of the five occurred.


What it must not be confused with

A variable holding undefined exists. A variable that was never declared does not, and the language reacts very differently to the two.

JAVASCRIPT
let known;
console.log(known);          // undefined, all is well

console.log(unknown);        // ReferenceError: unknown is not defined

console.log(typeof unknown); // "undefined", and no error at all

That third line is the only way to ask about a variable you are not sure exists without crashing the script. It is a historical use of typeof, still handy for detecting a third-party script.

Good to know

A default parameter value only fires on undefined. Writing function f(name = "visitor") then calling f(null) yields null, not "visitor": only a missing argument triggers the fallback.


Three ways to guard against it

FormWhat it doesWhen to pick it
value === undefinedTests undefined onlyTelling it apart from a null sent by a server
value ?? fallbackReplaces undefined and nullThe everyday case, a backup value
object?.propertyStops the read instead of crashingWalking through outside data

The second row deserves particular emphasis. Plenty of older code writes value || fallback, which also replaces zero and the empty string, two values that are often perfectly legitimate. Nullish coalescing (??) was added to the language precisely to fix that defect.


Frequently asked questions

Question

Why does my function return undefined?

Because no return was reached. Either the function has none, or an execution path walks past it, an if whose branches return nothing for instance. The language's fallback return value is always undefined, never an error, which keeps the problem silent until it reaches the screen.


Question

Can undefined be assigned to a variable?

Technically yes, but it is discouraged. Writing x = undefined imitates by hand something the engine already does, and it muddies the reading: whoever revisits the code cannot tell whether the emptiness was intended. For a deliberate blank, null expresses the intention far more clearly.


Question

Why does undefined show up in my interface?

Because a missing value was concatenated into text, where it turns into the string "undefined". The fix belongs at the source, as a fallback on the data rather than on the display. These fragile property chains are a full chapter of our JavaScript course, which starts from the errors people actually hit rather than from theory.

Related terms

Discover our javaScript glossary

Every word of JavaScript explained simply: keywords, built-in objects, methods, errors and concepts. Clear definitions and examples that actually run, to learn and to troubleshoot.

Share this article

Want to help us? Share this article on your networks or even better: on your site, in an article or in your newsletter.