What's the difference between "null" and "undefined"?


What is the difference between null and undefined?

They seem to mean the same thing.

If you write, const myVar = undefined;, is that any different from const myVar;?


undefined means a variable has been declared but has not yet been assigned a value. On the other hand, null is an assignment value. It can be assigned to a variable as a representation of no value. Also, undefined and null are two distinct types: undefined is a type itself (undefined) while null is an object.


Thanks. That answered my question, but I have another.

What happens if you say, let myVar = undefined;?


Nothing really, as it is unnecessary to define a variable as undefined, considering it would be undefined anyway. It’s a pointless line of code.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.