What is undefined?
Undefined is a property with global scope. Undefined is a variable in global scope; its initial property is undefined. Undefined means a variable with no value assigned. Sometimes a function can also return the value undefined when the variables in it don't contain any value. Also, a function returns undefined when it doesn't return any value.
Description
Undefined is a property you can check in conditional statements like it else statements.
You can assign undefined to a variable also.
E.g. var a = undefined;
Example
var x;
if(x === undefined)
{
//This statement execute
}
else
{
//This statement do not execute
}
Using typeof operator
var y;
if(typeof x === undefined)
{
//This will get execute
}
else
{
//This do not execute
}
let c;
console.log(c) //will return undefined
Difference between Null and Undefined
- Null means the value doesn’t exist or is not assigned explicitly.
- Undefined means the variable is declared but value is not assigned.
- Null has no specific type.
- Undefined is a type of undefined.
- Null is an object.
- Undefined is a property or type.
- Null represents an intentional absence of value.
- Whereas undefined represents an uninitialised variable.
- Null keyword is used.
- Undefined keywords are used.
Example :
OUTPUT: