undefined
- When a variable is declared but not initialized, or when a function does not return a value, the variable or the function’s result is
undefined
. - Accessing an object property or array element that does not exist also results in
undefined
. - It is a primitive value.
Example: In the example, we have shown undefined.
Javascript
let x; // variable declared but not initialized console.log(x); // Output: undefined function doSomething() { // no return statement, so the function returns undefined } console.log(doSomething()); // Output: undefined let obj = {}; console.log(obj.property); // Output: undefined |
Output
undefined undefined undefined
Undefined Vs Null in JavaScript
In JavaScript, both undefined
and null
represent the absence of a meaningful value, but they are used in slightly different contexts. In this article, we are going to learn the difference between Undefined and null in JavaScript.