I don't really have an answer, but according to Nicholas C. Zakas, page 30 of his book "Professional JavaScript for Web Developers":
Answer from bbg on Stack OverflowWhen defining a variable that is meant to later hold an object, it is advisable to initialize the variable to
nullas opposed to anything else. That way, you can explicitly check for the valuenullto determine if the variable has been filled with an object reference at a later time
Videos
I don't really have an answer, but according to Nicholas C. Zakas, page 30 of his book "Professional JavaScript for Web Developers":
When defining a variable that is meant to later hold an object, it is advisable to initialize the variable to
nullas opposed to anything else. That way, you can explicitly check for the valuenullto determine if the variable has been filled with an object reference at a later time
At the end of the day, because both null and undefined coerce to the same value (Boolean(undefined) === false && Boolean(null) === false), you can technically use either to get the job done. However, there is right way, IMO.
Leave the usage of
undefinedto the JavaScript compiler.undefinedis used to describe variables that do not point to a reference. It is something that the JS compiler will take care for you. At compile time the JS engine will set the value of all hoisted variables toundefined. As the engine steps through the code and values becomes available the engine will assign respective values to respective variables. For those variables for whom it did not find values, the variables would continue to maintain a reference to the primitiveundefined.Only use null if you explicitly want to denote the value of a variable as having "no value".
As @com2gz states:
nullis used to define something programmatically empty.undefinedis meant to say that the reference is not existing. Anullvalue has a defined reference to "nothing". If you are calling a non-existing property of an object, then you will getundefined. If I would make that property intentionally empty, then it must benullso you know that it's on purpose.
TLDR; Don't use the undefined primitive. It's a value that the JS compiler will automatically set for you when you declare variables without assignment or if you try to access properties of objects for which there is no reference. On the other hand, use null if and only if you intentionally want a variable to have "no value".
Sidebar: I, personally, avoid explicitly setting anything to undefined (and I haven't come across such a pattern in the many codebases/third party libs I've interacted with). Also, I rarely use null. The only times I use null is when I want to denote the value of an argument to a function as having no value, i.e.,:
function printArguments(a,b) {
console.log(a,b);
}
printArguments(null, " hello") // logs: null hello
undefined means a variable has been declared but has not yet been assigned a value :
var testVar;
console.log(testVar); //shows undefined
console.log(typeof testVar); //shows undefined
null is an assignment value. It can be assigned to a variable as a representation of no value :
var testVar = null;
console.log(testVar); //shows null
console.log(typeof testVar); //shows object
From the preceding examples, it is clear that undefined and null are two distinct types: undefined is a type itself (undefined) while null is an object.
Proof :
console.log(null === undefined) // false (not the same type)
console.log(null == undefined) // true (but the "same value")
console.log(null === null) // true (both type and value are the same)
and
null = 'value' // Uncaught SyntaxError: invalid assignment left-hand side
undefined = 'value' // 'value'
The difference can be explained with toilet tissue holder:
A non-zero value is like a holder with roll of toilet tissue and there's tissue still on the tube.
A zero value is like a holder with an empty toilet tissue tube.
A null value is like a holder that doesn't even have a tissue tube.
An undefined value is similar to the holder itself being missing.

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.
The question isn't really "why is there a null value in JS" - there is a null value of some sort in most languages and it is generally considered very useful.
The question is, "why is there an undefined value in JS". Major places where it is used:
- when you declare
var x;but don't assign to it,xholds undefined; - when your function gets fewer arguments than it declares;
- when you access a non-existent object property.
null would certainly have worked just as well for (1) and (2)*. (3) should really throw an exception straight away, and the fact that it doesn't, instead of returning this weird undefined that will fail later, is a big source of debugging difficulty.
*: you could also argue that (2) should throw an exception, but then you'd have to provide a better, more explicit mechanism for default/variable arguments.
However JavaScript didn't originally have exceptions, or any way to ask an object if it had a member under a certain name - the only way was (and sometimes still is) to access the member and see what you get. Given that null already had a purpose and you might well want to set a member to it, a different out-of-band value was required. So we have undefined, it's problematic as you point out, and it's another great JavaScript 'feature' we'll never be able to get rid of.
I actually use undefined when I want to unset the values of properties no longer in use but which I don't want to delete. Should I use null instead?
Yes. Keep undefined as a special value for signaling when other languages might throw an exception instead.
null is generally better, except on some IE DOM interfaces where setting something to null can give you an error. Often in this case setting to the empty string tends to work.
Best described here, but in summary:
undefined is the lack of a type and value, and null is the lack of a value.
Furthermore, if you're doing simple '==' comparisons, you're right, they come out the same. But try ===, which compares both type and value, and you'll notice the difference.