In the world of JavaScript and TypeScript, few concepts create as much quiet confusion as undefined and null. Both seem to represent “nothing,” yet they are not the same. Misunderstanding the distinction can lead to subtle bugs and unexpected behavior. Let’s clear the air once and for all.
Think of them as two different kinds of “emptiness.” One is accidental, the other is intentional. Mastering this difference is a key step toward writing cleaner, more predictable code.
Meet undefined: The Sound of Silence
undefined is a primitive value that TypeScript (and JavaScript) uses to signify that a variable has been declared but has not been assigned a value. It’s the default state of “not yet initialized.” It’s the system telling you, “I have a space for this, but nothing’s in it yet.”