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


#1

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;?


#2

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.


#3

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

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


#4

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.


#5

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