0% found this document useful (0 votes)
8 views3 pages

19 - Advanced Control Flow

This document discusses advanced control flow in JavaScript, specifically focusing on eliminating verbose if conditions by using object mapping for status messages. It explains implicit conversion in boolean contexts, detailing how non-boolean values are automatically converted to boolean and introduces falsy values that evaluate to false. Additionally, it covers the logical NOT operator for toggling boolean values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

19 - Advanced Control Flow

This document discusses advanced control flow in JavaScript, specifically focusing on eliminating verbose if conditions by using object mapping for status messages. It explains implicit conversion in boolean contexts, detailing how non-boolean values are automatically converted to boolean and introduces falsy values that evaluate to false. Additionally, it covers the logical NOT operator for toggling boolean values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

My notes https://learnjavascript.online/app.html?

id=1608

Advanced control flow


In this lesson, we're going to touch on a common scenario where you can completely get rid of if conditions
and have far better maintainable code

Let's say you're building an app like Deliveroo, and you have a method that sends push notifications to your
users, updating them on the progress of their order:
const getPushMessage = status => {
if (status === "received") {
return "Restaurant started working on your order.";
} else if (status === "prepared") {
return "Driver is picking up your food."
} else if (status === "en_route") {
return "Driver is cycling your way!";
} else if (status === "arrived") {
return "Enjoy your food!";
} else {
return "Unknown status";
}
}

This is a very verbose way to deal with it. We can refactor it by creating an object that contains all possible
messages, with the key of that object being the status. Let's refactor it:

const getPushMessage = status => {


const messages = {
received: "Restaurant started working on your order.",
prepared: "Driver is picking up your food.",
en_route: "Driver is cycling your way!",
arrived: "Enjoy your food!"
};

return messages[status] ?? "Unknown status";


}

Notice how we also used nullish coalescing to handle the else case when the status is different than the ones
we provided

Notice how we also used nullish coalescing to handle the else case when the status is different than the ones
we provided.

In this lesson, we'll explain how implicit conversion happens in boolean contexts. For example, we know that
the if statement expects a condition that evaluates to a boolean. But what happens when we give it a string or
a number?:
const name = "Sam";
const number = 0;

if (name) {
console.log("First condition");
}

if (number) {
console.log("second condition")
}

1 of 3 5/10/2025, 9:43 PM
My notes https://learnjavascript.online/app.html?id=1608

Implicit conversion

The if statement expects a boolean. However, when you provide it with a value of another type, it will
automatically convert it to a boolean. This is called implicit conversion because the conversion is occurring
automatically

How are the values converted to boolean? Who decides that "Sam" is true, while "" is false, and 30 is true
while 0 is false?

This is where Falsy values come into place.

Falsy values

false (is already a boolean)

null

undefined

NaN

""

"" (empty string)

These values above are called falsy values because, when converted to boolean, they will be converted to
false

Logical NOT operator (!)

If you'd like to convert a boolean value to its opposite, you can use the ! operator (Logical NOT operator).
Here's how it works:

!true; // false
!false; // true

// read it as: if NOT name


if (!name) {
//
}

Recap
Implicit conversion happens when JavaScript expects a boolean value but is given a non-boolean value.
Implicit conversion means that JavaScript will automatically convert the value to boolean.
Falsy values are converted to false. Everything else is converted to true.
Most common falsy values are: false, null, undefined, 0, "", NaN.
The logical NOT operator ! converts a boolean value to its opposite.

Chapter Recap

Implicit conversion happens when JavaScript expects a boolean value but is given a non-boolean value.
Implicit conversion means that JavaScript will automatically convert the value to boolean.

2 of 3 5/10/2025, 9:43 PM
My notes https://learnjavascript.online/app.html?id=1608

Falsy values are converted to false. Everything else is converted to true.


Most common falsy values are: false, null, undefined, 0, "", NaN.
The logical NOT operator ! converts a boolean value to its opposite.

Show previous notes

3 of 3 5/10/2025, 9:43 PM

You might also like