MeshWorld India LogoMeshWorld.

Difference between undefined, null and undeclared in JavaScript

Listen to ArticleAI Speech
~3 min read narration
100%
Difference between undefined, null and undeclared in JavaScript

In JavaScript, understanding how variables are handled by the engine is crucial for writing bug-free code. Almost everything in JavaScript is an object (or behaves like one), but how we declare and initialize them determines their state.

Let’s break down the differences between undefined, null, and undeclared with industrial examples.


1. Undefined

undefined means a variable has been declared but has not yet been assigned a value. It’s the default value the JavaScript engine gives to variables.

Example: Variable Declaration

JAVASCRIPT
let country;
console.log(country); // Output → undefined

country = "India 🇮🇳";
console.log(country); // Output → India 🇮🇳

Example: Object Property

JAVASCRIPT
let person = {
  firstName: undefined,
};
console.log(person.firstName); // Output → undefined

person.firstName = "Krishna 🙏";
console.log(person.firstName); // Output → Krishna 🙏

2. Null

null is an assignment value. It is used to represent the intentional absence of any object value. It must be explicitly assigned by the developer.

Example: Explicit Initialization

JAVASCRIPT
let country = null;
console.log(country); // Output → null
console.log(typeof country); // Output → object (Historical JS Bug)

country = "India 🇮🇳";
console.log(country); // Output → India 🇮🇳

3. Undeclared

A variable is undeclared when it has not been defined using var, let, or const. Accessing an undeclared variable (except with typeof) will throw a ReferenceError.

Example: Accessing Undeclared Variables

JAVASCRIPT
// This will throw an error immediately
try {
  console.log(planet);
} catch (e) {
  console.log(e.name); // Output → ReferenceError
}

console.log(typeof planet); // Output → undefined (Safe check)

Quick Comparison Table

FeatureUndefinedNullUndeclared
MeaningDeclared, no valueExplicitly emptyNot declared at all
Typeundefinedobjectundefined (via typeof)
Error?NoNoYes (ReferenceError)
Best UseDefault stateResetting a valueAvoid at all costs

Best Practices

  1. Avoid undefined assignments: Don’t manually set x = undefined. Use null instead.
  2. Use Strict Mode: Always use 'use strict'; or modern ES Modules to prevent accidental undeclared variable assignments to the global scope.
  3. Check for existence: Use if (variable == null) to check for both null and undefined simultaneously (due to type coercion).

Hope you learned something new! If this article was helpful, feel free to share it with your fellow engineers.

Happy coding!

Reader Quality Feedback

Did this technical guide help solve your problem?

Suggest Errata ($0)
Vishnu
Primary Author

Vishnu

Founder & Principal Architect at MeshWorld. Senior engineer and instructor specializing in AI agent systems, scalable web architecture, and modern development workflows.

Explore Author Archive
Compute Fuel & Open Testbed
100% Independent & Verified

Fuel High-Density, Zero-Fluff Engineering Deep-Dives

Every guide on MeshWorld is validated on physical Linux nodes and reproducible testbeds. If this article saved you hours of debugging or unblocked production, consider funding our next cluster run.

Weekly Dispatch

Join MeshWorld Dispatch

Get practical tutorials, system blueprints, and curated AI engineering notes straight to your inbox. No fluff, zero spam.

Zero spam. 1-click unsubscribe anytime.Prefer RSS?
Curated Continuations

Up Next in This Domain.

Browse Full Archive