[Javascript](EN) Difference between undefined and null
Difference between undefined and null
Environment and Prerequisite
- Javascript
undefined and null
undefined
- It is a primitive value used when a variable has not been assigned a value.
- A function returns undefined if explicitly nothing is returned.
- One of primitive types
// test default
let test;
console.log('variable test is ' + test);
// if statement
if (test === undefined) {
console.log('variable test is ' + test);
}
// if statement
if (undefined) {
console.log('undefined is considered as true');
} else {
console.log('undefined is considered as false');
}
// type test in if statement
// return type of typeof function is string
if (typeof(test) === 'undefined') {
console.log('variable test value is ' + test + ' and type is ' + typeof(test));
}
// function return
function testReturnNothing() {
let c;
}
console.log('testReturnNothing returns ' + testReturnNothing());
// check which is not defined
if (typeof(notDefinedVariable) === 'undefined') {
console.log('variable notDefinedVariable is not defined');
}
variable test is undefined
variable test is undefined
undefined is considered as false
variable test value is undefined and type is undefined
testReturnNothing returns undefined
variable notDefinedVariable is not defined
null
- It is a primitive value that represents the intentional absence of any object value.
- In APIs, null is often retrieved in a place where an object can be expected but no object is relevant.
- Treated as falsy for boolean operations.
// test default
let test = null;
console.log('variable test is ' + test);
// if statement
if (test === null) {
console.log('variable test is ' + test);
}
// if statement
if (null) {
console.log('null is considered as true');
} else {
console.log('null is considered as false');
}
// type test in if statement
// return type of typeof function is string
// its return type is not 'null' it is 'object'
if (typeof(test) === 'object') {
console.log('variable test value is ' + test + ' and type is ' + typeof(test));
}
// function return
function testReturnNull() {
return null;
}
console.log('testReturnNull returns ' + testReturnNull());
variable test is null
variable test is null
null is considered as false
variable test value is null and type is object
testReturnNull returns null
Comparison
undefined
- Value which represents not defined yet
null
- Intentional value that it is absence
etc
- It is equal when use
==
operator but it will be not equal when use===
operator. - Because
===
operator compare not only value but also type. Its type is different like below.
typeof(null) // object
typeof(undefined) // undefined
null === undefined // false
null == undefined // true
!null // true
!undefined // true
Reference
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined
- https://tc39.es/ecma262/#sec-undefined
- https://tc39.es/ecma262/#sec-ecmascript-language-types-undefined-type
- https://tc39.es/ecma262/#sec-undefined-value
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null
- https://tc39.es/ecma262/#sec-null-value
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality