Undefined and Null

undefined

In JavaScript, a variable that has been declared but has not been assigned a value is undefined. For example:

Loading Code . . .

Here, we declared a variable myVariable but did not assign any value to it. When we try to log the value of myVariable to the console, we get undefined.

For sure, you can define undefined as a value that represents the absence of any value.

Loading Code . . .

null

On the other hand, null is a value that represents the intentional absence of any object value. It is often used as a placeholder or to indicate that a value is intentionally missing. For example:

Loading Code . . .

Here, we assigned the value null to the variable myVariable. When we log the value of myVariable to the console, we get null.

It's worth noting that null is also considered an object in JavaScript (we'll see about objects in the following lessons), which is a bit of a historical quirk.

But from a practical standpoint, you can think of null as a way to represent an intentional absence of a value, while undefined is what you get when you haven't assigned any value to a variable yet.


Now select the type of each variable based on the value assigned to it.

let myVariable;

let age = 25;

let name = "John";

let isOpen = true;

let myVariable = null;

© 2024 - ®Mewters