As Software Engineers, we often see the value undefined or null, but what is the difference?
Both are primitive data types. Both are falsy values.
typeof null;
returns an object
typeof undefined;
returns undefined
null == undefined;
returns true
null === undefined;
returns false
The main difference between those two values is that I intentionally set null to a variable, and a declared variable without a value will always return undefined.
let name = null;
returns null
let age;
returns undefined
Sure, you can intentionally set undefined to a variable, but this is not a good way how you should code. If another developer in your team starts to work with your code and gets undefined, the developer would think that this variable has no value assigned. It would mislead him into different ways of solving this issue.
Declaring a variable and assigning a variable are two different things.
Latest comments (0)