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
-
null
is an empty or non-existent value. -
null
must be assigned - When you assign
null
to 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
null
andundefined
are 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
falsy
values. -
null
!==undefined
(strict equality) , butnull
==undefined
( loose equality)
Top comments (0)