Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is ?? in JavaScript

Understanding the ?? Operator in JavaScript

An Introduction to the ?? Operator

In JavaScript, the ?? operator is known as the nullish coalescing operator. It's a logical operator that returns its right-hand side operand when its left-hand side operand is null or undefined. In other words, it's a way to provide a default value for a variable or an expression that might be null or undefined.

Now, if you're new to programming, the terms "operand", "null", or "undefined" might sound intimidating, but don't worry. Let's break them down:

  • An "operand" is just a term for something we can operate on. In the equation 2 + 2, for example, 2 is an operand, and so is the other 2. The + is the operator.
  • "Null" in JavaScript means no value or no object. It implies the absence of value.
  • "Undefined" in JavaScript means a variable has been declared but has not yet been assigned a value.

When to Use ?? Operator

Imagine you're making a sandwich. You prefer to use mayonnaise, but if there's no mayo in your fridge, you'll use butter instead. This is exactly how the ?? operator works. If the first option (mayonnaise) isn't available (or in JavaScript terms, is null or undefined), it'll use the second option (butter).

Here's how that might look in code:

let sandwichSpread = mayonnaise ?? butter;

In this case, sandwichSpread will be mayonnaise if mayonnaise is not null or undefined. If mayonnaise is null or undefined, sandwichSpread will be butter.

The Difference Between ?? and ||

If you're familiar with JavaScript, you might be thinking, "Wait a minute, isn't this what the || operator does?" Not quite. There's a subtle, but important difference.

The || operator, known as the logical OR operator, will return the second operand if the first one is falsy. "Falsy" is a term that encompasses more than just null and undefined. It includes other values like 0, NaN, false, and an empty string ("").

So, if we use the || operator instead of the ?? operator in our sandwich spread example:

let sandwichSpread = mayonnaise || butter;

In this case, if mayonnaise is 0, NaN, false, or an empty string, sandwichSpread will be butter, which might not be what we want. Maybe in our sandwich making world, mayonnaise being 0 or false has a special meaning that we want to preserve.

Practical Examples of Using ??

Let's see a practical example of using ?? operator. Suppose you're building a function to greet a user:

function greet(name) {
    let greeting = name ?? "Stranger";
    console.log("Hello, " + greeting + "!");
}

greet("Alice");  // Outputs: "Hello, Alice!"
greet(null);     // Outputs: "Hello, Stranger!"
greet();         // Outputs: "Hello, Stranger!"

In this example, if the name parameter is not provided or is null, the function will use the string "Stranger" as a default value.

Conclusion: Embracing the ?? Operator

Like the perfect backup ingredient in your sandwich, the ?? operator provides a powerful way to ensure your JavaScript code can handle null and undefined values gracefully. By understanding and using the nullish coalescing operator, you can write more robust and predictable code. So, next time when you're coding and you might have a null or undefined situation, remember the ?? operator, your perfect fallback buddy. It's like having a spare key to your house. You hope you never need it, but you'll be glad it's there when you do.