Null is an assignment value. It can be assigned to a variable as a representation of no value:
There are two features of null you should understand
-
nullis an empty or non-existent value. -
nullmust be assigned - When you assign
nullto a variable, you are declaring that this value is explicitly empty.
let age = null;
console.log(age); // null
Undefined usually means a variable has been declared, but not defined.
When you try to access a property of an object that doesn't exist you will get undefined in return.
const shashank = {
name : 'Shashank',
}
console.log(shashank.age) // undefined
- Both
nullandundefinedare primitive values in JavaScript. - When you do loose equality check on both it returns
true
null == undefined // true
because both are considered falsy values.
Summary
- null is an assigned value. It means
nothing. - undefined typically means a variable has been declared but not defined yet.
- null and undefined are
falsyvalues. -
null!==undefined(strict equality) , butnull==undefined( loose equality)
Top comments (0)