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
let country;
console.log(country); // Output โ undefined
country = "India ๐ฎ๐ณ";
console.log(country); // Output โ India ๐ฎ๐ณ When you log a variable that is only declared (using let or var), the engine returns undefined because no memory has been allocated for a specific value yet.
Example: Object Property
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
let country = null;
console.log(country); // Output โ null
console.log(typeof country); // Output โ object (Historical JS Bug)
country = "India ๐ฎ๐ณ";
console.log(country); // Output โ India ๐ฎ๐ณ typeof null returns "object". This is a well-known bug in JavaScript that dates back to its first version. It wasnโt fixed to avoid breaking the existing web.
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
// 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) Never use variables before declaring them. While var allows hoisting, let and const live in the Temporal Dead Zone (TDZ) until they are declared, leading to crashes if accessed early.
Quick Comparison Table
| Feature | Undefined | Null | Undeclared |
|---|---|---|---|
| Meaning | Declared, no value | Explicitly empty | Not declared at all |
| Type | undefined | object | undefined (via typeof) |
| Error? | No | No | Yes (ReferenceError) |
| Best Use | Default state | Resetting a value | Avoid at all costs |
Best Practices
Always explicitly initialize your variables. If you donโt have a value yet, use null. This makes it clear to other developers that the variable is intentionally empty, rather than accidentally uninitialized.
- Avoid
undefinedassignments: Donโt manually setx = undefined. Usenullinstead. - Use Strict Mode: Always use
'use strict';or modern ES Modules to prevent accidental undeclared variable assignments to the global scope. - Check for existence: Use
if (variable == null)to check for bothnullandundefinedsimultaneously (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!
Related Articles
Deepen your understanding with these curated continuations.
Convert array to an object in JavaScript
This article explains simplest and quickest way to convert array to an object in JavaScript. Using widely accepted spread operator `...` makes easy to do it.
How to Update an Array Element in JavaScript
Learn the simplest ways to update array elements in JavaScript. This guide explains how to use assignment operators and modern methods to modify array values.
Flatten an Array One Level Deep in Javascript
This article explains to flatten an array one level deep in javascript comparing with lodash _.flatten method.