Open In App

What is Undefined in JavaScript ?

In JavaScript, undefined is a primitive value that is automatically assigned to variables that have been declared but not yet initialized with a value. It’s also the default return value of functions that do not explicitly return anything.

Here are some key points about undefined:



  1. It represents the absence of a value.
  2. It’s a primitive data type.
  3. It’s automatically assigned to variables that have been declared but not yet assigned a value.
  4. It’s returned when accessing properties of an object that do not exist.
  5. It’s considered a falsy value, meaning it evaluates to false in a Boolean context.

Example: Here, the variable x is declared but not assigned a value, so it automatically gets the value undefined. In the second example, the function greet() is called without passing any arguments, so the parameter name inside the function has the value undefined.




let x;
console.log(x); // Output: undefined
 
function greet(name) {
    console.log(`Hello, ${name}!`);
}
 
greet(); // Output: Hello, undefined!

Output

undefined
Hello, undefined!
Article Tags :