0x7f Inc.
Undefined vs. null
This is a very interesting today I learned about JavaScript.
Let’s say you have a function friendEmail
:
class User {
get friendEmail() {
const { sharedEmail } = this.data;
return sharedEmail ? this.data.email : undefined;
}
}
Reviewing this, as I did, you might leave a comment saying:
Should we return null here instead of undefined?
And you would we wrong! As per TC39 documentation about null
1 you can see the following:
4.4.15 null value
primitive value that represents the intentional absence of any object value
So a function that returns a primitive2 should in the abstinence of value return an undefined
and a function that returns an object type should return null
.
Keep in mind that returning a null wont however be switching return types, since null
it self is a primitive but it only represents absence of any object value.
Weird stuff, right?