What are the differences between null and undefined?
The differences between null and undefined are given below:
Null | Undefined |
---|---|
It indicates that variable points to no object. | It indicates that variable has been declared but not yet assigned a value. |
Type of null is object | Type of undefined is undefined |
The null value is a primitive value | The undefined value is a primitive value |
Indicates the absence of a value for a variable | Indicates absence of variable itself |
Converted to zero (0) while performing primitive operations | Converted to NaN while performing primitive operations |
var userName; console.log(userName); //shows undefined console.log(typeof userName); //shows undefined | var userName = null; console.log(userName); //shows null console.log(typeof userName); //shows object |
Special cases about null and undefined
null === undefined // false null == undefined // true null === null // true
null = 'value' // ReferenceError undefined = 'value' // 'value'