getify / You-Dont-Know-JS

A book series on JavaScript. @YDKJS on twitter.
Other
177.9k stars 33.39k forks source link

null vs. undefined #1774

Closed Violet-Bora-Lee closed 2 years ago

Violet-Bora-Lee commented 2 years ago

Yes, I promise I've read the Contributions Guidelines (please feel free to remove this line).


Please type "I already searched for this issue":

I already searched for this issue

Edition: (1st or 2nd) 2nd

Book Title: Book1, Get Started

Chapter: Chapter 2: Surveying JS

Section Title: Values

Question: According to the resources I've read, it's not recommended to use undefined rather than null.

It contradicts the contents of the section.

Many developers prefer to treat them both consistently in this fashion, which is to say that the values are assumed to be indistinguishable. If care is taken, this is often possible. However, it's safest and best to use only undefined as the single empty value, even though null seems attractive in that it's shorter to type!

Could you more elaborate?

FYI. I'm translating the books into Korean with the agency.

getify commented 2 years ago

Different authors have different opinions.

Violet-Bora-Lee commented 2 years ago

I got it! I just wanted double check. Thanks for your rapid response 👍