It is a one of the javascript primitive types.
Javascript will assign undefined
to any variable or object's property which is not declared.
undefined
is one of the falsy values in javascript like null
.
let a = {};
console.log(a.test); // undefined
In the above snippet test
property is undefined cause javascript assigns undefined to variables or object's properties which declared but not assigned any value.
a.test1 = undefined;
console.log(test1); // undefined
console.log(a); // { test1: undefined }
In the above snippet, you are specifically assigning undefined to test1
property of a
. If you don't assign any value then it will contain undefined by default JS behavior as shown below
a.test1;
console.log(a); // { test1: undefined }
One more interesting feature of undefined is with JSON. JSON.stringify will ignore all properties of object which are undefined.
let obj = {a:1, b:undefined};
console.log(JSON.stringify(obj)); // {a:1}
For more about primitive types undefined