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.

Similar Reads

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....

null

...

Difference between undefined and null

It is a deliberate assignment that represents the absence of any object value. It is often used to explicitly indicate that a variable or object property should have no value or no reference to any object. It is also a primitive value....