Created
May 11, 2016 10:27
-
-
Save cyan33/fab86a373b8c4dee16ecdb4315b9c7f6 to your computer and use it in GitHub Desktop.
The difference between 'undefined' and 'null' in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In JavaScript, undefined means a variable has been declared but has not yet been assigned a value, such as: | |
var TestVar; | |
alert(TestVar); //shows undefined | |
alert(typeof TestVar); //shows undefined | |
null is an assignment value. It can be assigned to a variable as a representation of no value: | |
var TestVar = null; | |
alert(TestVar); //shows null | |
alert(typeof TestVar); //shows object | |
From the preceding examples, it is clear that undefined and null are two distinct types: undefined is a type itself (undefined) while null is an object. | |
null === undefined // false | |
null == undefined // true | |
null === null // true | |
and | |
null = 'value' // ReferenceError | |
undefined = 'value' // 'value' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Second thought tho,
null
is assigned intentionally, butundefined
is not.