Course

Null and Undefined

Loading...

Learning Objective: Differentiate between and values and understand their unique roles in JavaScript.


What is ?

is a special value in JavaScript used to represent “nothing,” “empty,” or “value unknown.” It is intentionally set by a programmer to indicate that a variable should have no value.

Example: Assigning

let age = null / This means "age is unknown or empty."
let age = null; // This means "age is unknown or empty."
console.log(age); // null

Key Points:

  • is a primitive value in JavaScript.
  • It is not an object (although typeof null returns "object" due to a historical quirk).

What is ?

is the default value for variables that have been declared but not assigned a value.

Example: Uninitialized Variables

let x; // No value assigned
console.log(x); // undefined

Can you assign ?

While it’s technically possible, it’s not recommended to assign manually. Use for this purpose instead.

let x = 123;
x = undefined; // Not recommended
console.log(x); // undefined

Key Differences Between and

Feature

Purpose

Represents “no value” (intentional).

Default value for uninitialized variables.

Assigned By

Programmer.

JavaScript.

Type

(JavaScript quirk).

(its own type).

Understanding the Behavior

  1. means a variable exists but doesn’t have a value:

    let name; // Declared but not assigned
    console.log(name); // undefined
  2. means the variable intentionally has no value:

    let name = null; // Explicitly set to "no value"
    console.log(name); // null
  3. They are not strictly equal:

    console.log(null === undefined); // false
  4. But they are loosely equal:

    console.log(null == undefined); // true

Using to Inspect Types

You can inspect the type of any value using :

let empty = null;
let notSet;

console.log(typeof empty); // "object" (a JavaScript quirk!)
console.log(typeof notSet); // "undefined"

Interactive Activity

  1. Open VS Code and create a file called .
  2. Write the following:
    • Declare a variable with to represent “no value.”
    • Declare another variable without assigning a value to make it .
    • Use to inspect the type of both variables.
    • Log the results to the console.

Example:

let empty = null; // null value
let notSet; // undefined

console.log(empty); // null
console.log(typeof empty); // "object"

console.log(notSet); // undefined
console.log(typeof notSet); // "undefined"
  1. Change the value of the variable to something else and observe the changes.

What’s Next?

In this lesson, you learned the difference between and . These concepts are foundational for debugging and writing clear, intentional code. Practice working with them, and don’t forget to explore their behavior using !

Next, we’ll explore objects, a key data type in JavaScript that enables you to store and manage collections of data. Let’s keep going! 🚀


Loading...

0 Comments

"Please login to view comments"

glass-bbok

Join the Conversation!

Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.

Upgrade your account
tick-guideNext Lesson

Objects in JS