Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is the question mark in JavaScript

Peeking into the Question Mark in JavaScript

If you’ve been exploring JavaScript and coding for a bit, you might have stumbled upon the odd use of a question mark (?). No, JavaScript isn’t questioning you. It's a key aspect of the language that many beginners might not be familiar with. In this guide, we will dive deep into this topic.

Understanding the Basics

The question mark (?) in JavaScript is part of what we call a ternary operator. The term "ternary" comes from a Latin word "ternarius" which means "composed of three parts". So, a ternary operator is the one that works with three values. It's a shortcut for an if-else statement, which is a basic unit of decision making in programming.

Consider a simple if-else statement:

let isRaining = true;
let activity;

if (isRaining) {
    activity = "Stay indoors";
} else {
    activity = "Go for a walk";
}

console.log(activity); // Outputs: "Stay indoors"

Now, the same logic can be condensed using the ternary operator:

let isRaining = true;
let activity = isRaining ? "Stay indoors" : "Go for a walk";
console.log(activity); // Outputs: "Stay indoors"

In the example above, isRaining ? "Stay indoors" : "Go for a walk"; is the ternary operator. It checks if isRaining is true. If it's true, it chooses "Stay indoors", otherwise it chooses "Go for a walk".

Breaking Down the Ternary Operator

The ternary operator is made up of three parts: the condition, the if-true value, and the if-false value. It follows this structure: condition ? valueIfTrue : valueIfFalse.

  1. Condition: It's a statement that is either true or false. This is what the ternary operator checks. In our example, isRaining is the condition.
  2. Value If True (?): If the condition is true, this value is returned. In our example, "Stay indoors" is the value if true.
  3. Value If False (:): If the condition is false, this value is returned. In our example, "Go for a walk" is the value if false.

Multiple Ternary Operators

Just like if-else statements, ternary operators can be chained together. This is useful when you have multiple conditions to check. Here's an example:

let weather = "sunny";
let activity = weather === "raining" ? "Stay indoors" : weather === "sunny" ? "Go to the beach" : "Go for a walk";
console.log(activity); // Outputs: "Go to the beach"

In this example, we first check if weather is "raining". If it's true, we choose "Stay indoors". If it's false, we go to the next condition: is weather "sunny"? If it's true, we choose "Go to the beach". If it's false, we choose "Go for a walk".

A Note on Readability

While the ternary operator can make your code shorter, use it wisely. Chaining multiple ternary operators can make your code hard to read and understand, especially for beginners. As a rule of thumb, if your ternary expression is getting too complicated, it may be time to switch back to a good old if-else statement.

Conclusion

The question mark in JavaScript, forming the ternary operator, is a powerful tool that can make your code cleaner and more efficient. It's like a mini choose-your-own-adventure game: if this happens, do that, otherwise, do something else.

But remember, with great power comes great responsibility. Use it to make your code easier to understand, not more complicated. As you continue your journey in JavaScript, you'll find the balance that works best for you. Happy coding!