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
:
- It represents the absence of a value.
- It’s a primitive data type.
- It’s automatically assigned to variables that have been declared but not yet assigned a value.
- It’s returned when accessing properties of an object that do not exist.
-
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
.
Javascript
let x; console.log(x); // Output: undefined
function greet(name) {
console.log(`Hello, ${name}!`);
} greet(); // Output: Hello, undefined!
|
Output
undefined Hello, undefined!