r/JavaScriptTips • • Nov 11 '22

Why does JavaScript have both null and undefined?

Most programming languages have a single value to indicate the absence of something, which is often called null and is used to represent a variable that has no value associated with it.

But JavaScript is different. Someone who is just starting out with JavaScript or coming from a different language usually finds it hard to understand, why there are two values that indicate absence: null and undefined

Check out the post to learn how these two are different.

12 Upvotes

2 comments sorted by

2

u/Bgtti Nov 11 '22

Null is a defined value, while undefine is not. This link can help explain why null was created in JS: https://flexiple.com/javascript/undefined-vs-null-javascript/

1

u/Sorry-Programmer9826 Oct 05 '24

I love that most languages consider null to have been a mistake and are trying to patch it out with optionals etc, while javascript thinks null is so great it includes a second type of null for even more fun