Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is undefined in JavaScript

Getting Acquainted with 'undefined' in JavaScript

As you embark on your programming journey, you will encounter many concepts that may seem confusing at first. One such concept in JavaScript is 'undefined'. Don't worry, it's not as scary as it sounds. Let's break it down together.

A Curious Case of 'undefined'

'Undefined' in JavaScript is a special value that's assigned to variables when they are declared but not assigned any specific value. It's like a placeholder that JavaScript uses to say, "Hey, I know this variable exists, but it doesn't hold any value right now."

Here's a simple example to illustrate this:

let myVariable;
console.log(myVariable); // Output: undefined

As you see in the example above, myVariable is declared but no value is assigned to it. When we try to log the value of myVariable, we get 'undefined'.

'undefined' vs 'null'

In JavaScript, another special value that often gets mixed up with 'undefined' is 'null'. Although both seem to denote "nothingness", they are used in slightly different contexts.

'Null' is an assignment value that implies the absence of any object value. It's like you explicitly telling JavaScript, "This variable holds nothing". Here's how you can use 'null':

let myVariable = null;
console.log(myVariable); // Output: null

In this example, we are explicitly assigning the value 'null' to myVariable. When we log its value, we get 'null' as expected.

Functions and 'undefined'

'Undefined' also plays a vital role when dealing with functions in JavaScript. If a function does not explicitly return a value, it will return 'undefined' by default. This is JavaScript's way of saying, "This function didn't give me anything to return, so I'm just going to return 'undefined'."

Here's an example:

function greet() {
    console.log("Hello, world!");
}

let greeting = greet();
console.log(greeting); // Output: undefined

The function greet() doesn't have a return statement. So, when we call this function and assign its return value to greeting, we get 'undefined'.

Tricky Situations with 'undefined'

Sometimes, 'undefined' can pop up in places where you least expect it. This often happens when you try to access an object property or an array element that doesn't exist.

let myObject = {};
console.log(myObject.property); // Output: undefined

let myArray = [1, 2, 3];
console.log(myArray[5]); // Output: undefined

In both these examples, we are trying to access something that doesn't exist. JavaScript simply returns 'undefined' in such cases, indicating that the requested property or element is not present.

Checking for 'undefined'

If you ever need to check if a variable is 'undefined', JavaScript provides a simple way of doing it using the typeof operator.

let myVariable;
console.log(typeof myVariable === 'undefined'); // Output: true

In this example, typeof operator returns the string 'undefined', which we then compare with the string 'undefined' to verify if myVariable is indeed undefined.

Wrapping Up

To sum it all up, 'undefined' in JavaScript is like an empty box that hasn't been filled yet. It's JavaScript's way of saying: "I know this box exists, but there's nothing in it right now."

Remember, while 'undefined' might seem confusing at first, it's actually quite simple. It's just a placeholder for something that doesn't exist yet. So, the next time you see 'undefined' popping up in your code, don't be scared. Just think of it as an empty box that's waiting to be filled!