Mastering Conditional and Logical Operators and the forEach Loop in JavaScript

Overview

Hello! Today, we're going to dive into some key concepts of JavaScript. We will focus on the ternary conditional operator, logical AND and OR operators, and the forEach loop. What's awesome about these topics is that, although each one is a simple tool on its own, when used together they can significantly streamline your code and enhance its readability. So, whether you're developing interactive web pages, building web apps, or even gaming, mastering these tools will undoubtedly prove beneficial!

Understanding the Ternary Conditional Operator

A Ternary Operator in JavaScript serves as a shortcut for an if-else statement. It derives its name "ternary" from involving three parts: a condition, a true result, and a false result.

Here's how it appears:

let result = (condition) ? 'value if true' : 'value if false';

If the condition holds true, you receive the first result; otherwise, you obtain the second result.

For instance, consider that you're developing a game and want to set a message based on the player's score:

let score = 85;
let message = (score > 50) ? 'Victory!' : 'Try again.';
console.log(message); // 'Victory!'

In the example above, the game declares 'Victory!' when the player's score exceeds 50; otherwise, it encourages the player to 'Try again.'.

Let's consider a more complicated example where we use a nested ternary operator. Assume we want to classify a number as "negative", "positive", or "zero":

let number = 5;
let description = (number > 0) ? 'positive' : (number < 0) ? 'negative' : 'zero';
console.log(description); // 'positive'

In the above example, if number is greater than 0, we get 'positive'. If not, JavaScript checks if number is less than 0, in which case we get 'negative'. If both conditions fail (i.e., if the number is 0), we get 'zero'. We used a nested ternary operator to evaluate a condition in the else block.

Logical AND (`&&`) and OR (`||`) Operators Refresher

In JavaScript, logical operators are utilized to ascertain logic between variables or values. Today, we will examine two such operators: AND (&&) and OR (||).

The AND (&&) operator returns true only if all conditions hold true. This can be beneficial when multiple conditions need to be simultaneously fulfilled. For instance, consider an online store that provides a special discount for customers from a certain region who also have a specific item in their cart:

let customerRegion = 'North America';
let cartContains = 'Special Item';
let isDiscounted = (customerRegion === 'North America' && cartContains === 'Special Item');
console.log(isDiscounted); // true

Alternatively, the OR (||) operator returns true if at least one of the conditions holds true. This can be useful when an effect has multiple possible triggers. For example, assume that the game sound is activated either when the player clicks on the settings or when the player reaches a particular level:

let isSettingsClicked = true;
let isLevelUp = false;
let activateSound = (isSettingsClicked || isLevelUp);
console.log(activateSound); // true
Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal