Null and Undefined

Hey ,

I'm thrilled to help you learn JavaScript. Unfortunately, you've landed on a page where you cannot access with your current purchase.

Please upgrade (use this link) access this content.

I'm super eager to help you learn more!

Null and Undefined

Let’s cycle back to primitives. Earlier, you learned that there are seven possible primitives in JavaScript:

  1. String
  2. Number
  3. Boolean
  4. Null
  5. Undefined
  6. Symbol
  7. BigInt

We’ll focus on Null and Undefined values in this lesson.

Undefined

undefined is a value that indicates an absence of a value. We say that something is undefined when it is not explicitly defined.

For example, if you declare a variable but did not assign anything to it, the variable will be undefined.

let a
console.log(a) // undefined

If you have a function that returns nothing, the result of the function will be undefined.

const returnsNothing = () => {}

const test = returnsNothing()
console.log(test) // undefined

Null

null is a value that is used to indicate “nothingness”. Developers need to explicitly set a value to be null.

const zell = {
  firstName: 'Zell',
  middleName: null,
  lastName: 'Liew'
}

console.log(zell.middleName) // null

Null is different from undefined

If you compare null and undefined with the strictly equal operator, you’ll get false.

null === undefined // false

This is because null and undefined are different primitives.