null is a magic pointer

There Is No Spoon

Neo With Spoon

null is the pointer to an object that means "there is no object"

Null is useful

Null is used in cases where "nothing yet" is a valid scenario.

For instance, if a user has an account, but doesn't (yet) have a profile picture, user.profilePic may be null.

Then you can test for that case, e.g.

if (user.profilePic === null) {
} else {

Null is dangerous

let fruit = "apple"
fruit = null

Read the error!

Errors are good

They tell you

Interpret this error:

TypeError: Cannot read property 'toUpperCase' of null

null pointer errors

If You're Going To Fail...

Two philosophies:

Which idea is better?

Why or why not?

failure recovery: different modes for different roles

graceful - generally good for users

fail-fast - generally good for coders

JavaScript has several nulls

Docs: MDN: null

Next Lesson   Previous Lesson