JavaScript Logical Operators: A practical perspective in TypeScript
JavaScript Logical Operators: A practical perspective in TypeScript
By Anthony Bendeler
5 min read
This article approaches logical operators from a practical perspective by giving more real-life examples and describing guidelines on when to use what.
- Authors
- Name
- Anthony Bendeler
- linkedinAnthony Bendeler
When you think about building applications, a big part of it revolves around rules. Your code needs to do, show, and or return something. Often, we have to compare values with each other and write behavior based on certain conditions. We have a few operators in JavaScript that can help us with this.
||
The logical OR operator The logical OR operator comes in handy when you must choose between two expressions and choose either of the two that is truthy.
enum InvoiceStatus {
CLOSED = 'closed',
CANCELLED = 'cancelled',
OPEN = 'open',
PROCESSING = 'processing',
}
type Invoice = {
// ...rest
status: InvoiceStatus
}
const hasUnpaidInvoices = (invoices: Invoice[]): boolean =>
invoices.some(
(invoice) =>
invoice.status === InvoiceStatus.OPEN || invoice.status === InvoiceStatus.PROCESSING
)
&&
The logical AND operator Given that condition1
is true and condition2
is true, evaluate to true. Otherwise, evaluate to false. I use it in conjunction with the logical OR operator to keep data consistent, because, without it, you get either what you evaluate or the boolean value false, which is not what you actually might want to work with further down the chain. In TypeScript you are warned, which saves you some unexpected behavior.
type Product = {
id: string
price: number
}
type Basket = {
products: Product[]
}
type Customer = {
savedPoints: number
}
function determineTotalPrice(savedPoints: number, products: Product[]): number {
const discount = Math.floor(savedPoints / 100) * 5
const totalPrice = products.reduce(
(totalPrice, { price }) =>
// you could also use a ternary operator here, but this is just an example
(typeof price === 'number' && totalPrice + price) || totalPrice,
0
)
return totalPrice - discount
}
Notice I did some type checking on something that I declared already as a number. There are cases where you don't have control over the data that you will work with (e.g. external api). If you don't have some validation layer in between your business logic and a data dependency, you might want to build in some defensive programming. For simplicity's sake, I used only the price of Product as an example of this.
??
The nullish coalescing operator I personally am not a fan of using it and I avoid it as much as possible. It also helps that I am mostly a back-end developer (NestJS) and that I am using TypeScript.
As stated in the mdn web docs, it is a special logical OR operator, but it evaluates any non-truthy value other than null
and undefined
as truthy: ''
, 0
, NaN
and false
.
The practical question is: when would I prefer those non-truthy values over a default value? I have scanned some front-end projects and I have seen other developers use the operator where for me it doesn't make any sense.
export enum EnergyTypes {
Electricity = 'Electricity',
Gas = 'Gas',
Undefined = 'Undefined',
}
// data is undefined or an object
const data = useGetActiveEnergyType(ean).data
const activeEnergyType = data ?? EnergyTypes.Electricity
Since data can be undefined
, you might as well use ||
instead of ??
.
<StyledMenu
open={!!anchorElement}
anchorEl={anchorElement}
onClose={() => setAnchorElement(null)}
defaultValue={locale ?? 'nl'}
sx={{ left: { sx: '46px', lg: '-4px' } }}
>
{children}
</StyledMenu>
Since locale can be undefined
and maybe even an empty string, you don't want to keep the empty string, so you default to 'nl'
. Hence, ||
is a better candidate.
function someRenderFunction() {
return (
<TextField
inputRef={ref}
{...props}
{...fieldProps}
error={!!fieldState.error}
helperText={fieldState.error?.message ?? ' '}
onChange={handleOnChange}
/>
)
}
I am not sure if this is a typo, but the default value would be a string with a whitespace. This means that you would not want to keep an empty string in the first place. Again, ||
is the better choice.
const firstName = `${data?.firstName ?? ''}`
If for whatever reason firstName is false or an empty string and you default to an empty string, wouldn't ||
also just be the better choice?
My take on this operator is that it is heavily overused for cases where the OR operator is good enough when you make sure you can keep your data consistent.
Short-circuiting
There is also a difference on how these logical operators proceed in processing your code. The logical AND operator will not evaluate the expression on its right side if the expression on its left is already non-truthy.
The logical OR operator (and nullish coalescing operator) will not evaluate the expression on its right side if the left side is already truthy.
This might have a significant impact on your performance if an action or state depends on heavy calculations. Here is an example for the AND operator.
async function determineNextCourseofAction(customer) {
const history = await getHistory(customer)
if (customer.isReady && history.canContinue) {
return ACTION.PROCEED
}
return ACTION.WAIT
}
Though the code is easy to read, it is also inefficient if :
getHistory
makes a heavy call;getHistory
makes makes complex calculations with possibly a lot of data- the flow depends firstly on whether the customer is ready or not.
A better way to write this would be (including the ternary operator):
async function determineNextCourseofAction(customer) {
if (customer.isReady) {
const history = await getHistory(customer)
return history.canContinue ? ACTION.PROCEED : ACTION.WAIT
}
return ACTION.WAIT
}
Spaghetti Code
You can make it quite hard for a fellow developer if you make complicated evaluations.
const determineNextCourseOfAction = (order, customer) => {
return (order.status === 'open' && customer.balance > 0) || order.status === 'pending'
? 'sendOrder'
: 'sendReminderEmail'
}
I am more in favour of being verbose and explain what everything means in human language, even though this will result in more declarations:
const determineNextCourseOfAction = (order, customer) => {
const customerCanProceed = order.status === 'open' && customer.balance > 0
const orderIsAlreadyPaid = order.status === 'pending'
const canSendOrder = customerCanProceed || orderIsAlreadyPaid
return canSendOrder ? 'sendOrder' : 'sendReminderEmail'
}
Guidelines
Use logical AND only for when you have simple scenarios where you want to check on whether a condition is met. If the expression on the left of the operator is true, then the expression on the right will also be evaluated. If the expression on the left is false, then the right will not be evaluated.
Use logical OR for simple scenarios where your logic is dependent on either of the two expressions. If the expression on the left of the operator is true, then the expression on the right will not be evaluated.
Use a combination of logical AND and OR for more complex scenarios. Consider also using the ternary operator where possible, as this makes it easier to read in most cases.
Keep short circuiting in mind when you write your code. Make the least expensive evaluation the last one to evaluate if behaviour depends on either of the two.
Do not use
??
when you could also use||
. This depends on the scenarios where your default value would be meaningful, but wherefalse
,''
or0
should still be regarded as a valid and meaningful value as well. I would still recommend avoiding it, becauseNaN
will also be kept using??
.Do not explode your functions with complicated checks. Declare multiple variables that hold a specific value and combine them later. This will make your code more pleasant to read.
Upcoming events
The Test Automation Meetup
PLEASE RSVP SO THAT WE KNOW HOW MUCH FOOD WE WILL NEED Test automation is a cornerstone of effective software development. It's about creating robust, predictable test suites that enhance quality and reliability. By diving into automation, you're architecting systems that ensure consistency and catch issues early. This expertise not only improves the development process but also broadens your skillset, making you a more versatile team member. Whether you're a developer looking to enhance your testing skills or a QA professional aiming to dive deeper into automation, RSVP for an evening of learning, delicious food, and the fusion of coding and quality assurance! 🚀🚀 18:00 – 🚪 Doors open to the public 18:15 – 🍕 Let’s eat 19:00 – 📢 First round of Talks 19:45 – 🍹 Small break 20:00 – 📢 Second round of Talks 20:45 – 🍻 Drinks 21:00 – 🙋♀️ See you next time? First Round of Talks: The Power of Cross-browser Component Testing - Clarke Verdel, SR. Front-end Developer at iO How can you use Component Testing to ensure consistency cross-browser? Second Round of Talks: Omg who wrote this **** code!? - Erwin Heitzman, SR. Test Automation Engineer at Rabobank How can tests help you and your team? Beyond the Unit Test - Christian Würthner, SR. Android Developer at iO How can you do advanced automated testing for, for instance, biometrics? RSVP now to secure your spot, and let's explore the fascinating world of test automation together!
| Coven of Wisdom - Amsterdam
Go to page for The Test Automation MeetupCoven of Wisdom - Herentals - Winter `24 edition
Worstelen jij en je team met automated testing en performance? Kom naar onze meetup waar ervaren sprekers hun inzichten en ervaringen delen over het bouwen van robuuste en efficiënte applicaties. Schrijf je in voor een avond vol kennis, heerlijk eten en een mix van creativiteit en technologie! 🚀 18:00 – 🚪 Deuren open 18:15 – 🍕 Food & drinks 19:00 – 📢 Talk 1 20:00 – 🍹 Kleine pauze 20:15 – 📢 Talk 2 21:00 – 🙋♀️ Drinks 22:00 – 🍻 Tot de volgende keer? Tijdens deze meetup gaan we dieper in op automated testing en performance. Onze sprekers delen heel wat praktische inzichten en ervaringen. Ze vertellen je hoe je effectieve geautomatiseerde tests kunt schrijven en onderhouden, en hoe je de prestaties van je applicatie kunt optimaliseren. Houd onze updates in de gaten voor meer informatie over de sprekers en hun specifieke onderwerpen. Over iO Wij zijn iO: een groeiend team van experts die end-to-end-diensten aanbieden voor communicatie en digitale transformatie. We denken groot en werken lokaal. Aan strategie, creatie, content, marketing en technologie. In nauwe samenwerking met onze klanten om hun merken te versterken, hun digitale systemen te verbeteren en hun toekomstbestendige groei veilig te stellen. We helpen klanten niet alleen hun zakelijke doelen te bereiken. Samen verkennen en benutten we de eindeloze mogelijkheden die markten in constante verandering bieden. De springplank voor die visie is talent. Onze campus is onze broedplaats voor innovatie, die een omgeving creëert die talent de ruimte en stimulans geeft die het nodig heeft om te ontkiemen, te ontwikkelen en te floreren. Want werken aan de infinite opportunities van morgen, dat doen we vandaag.
| Coven of Wisdom Herentals
Go to page for Coven of Wisdom - Herentals - Winter `24 editionMastering Event-Driven Design
PLEASE RSVP SO THAT WE KNOW HOW MUCH FOOD WE WILL NEED Are you and your team struggling with event-driven microservices? Join us for a meetup with Mehmet Akif Tütüncü, a senior software engineer, who has given multiple great talks so far and Allard Buijze founder of CTO and founder of AxonIQ, who built the fundaments of the Axon Framework. RSVP for an evening of learning, delicious food, and the fusion of creativity and tech! 🚀 18:00 – 🚪 Doors open to the public 18:15 – 🍕 Let’s eat 19:00 – 📢 Getting Your Axe On Event Sourcing with Axon Framework 20:00 – 🍹 Small break 20:15 – 📢 Event-Driven Microservices - Beyond the Fairy Tale 21:00 – 🙋♀️ drinks 22:00 – 🍻 See you next time? Details: Getting Your Axe On - Event Sourcing with Axon Framework In this presentation, we will explore the basics of event-driven architecture using Axon Framework. We'll start by explaining key concepts such as Event Sourcing and Command Query Responsibility Segregation (CQRS), and how they can improve the scalability and maintainability of modern applications. You will learn what Axon Framework is, how it simplifies implementing these patterns, and see hands-on examples of setting up a project with Axon Framework and Spring Boot. Whether you are new to these concepts or looking to understand them more, this session will provide practical insights and tools to help you build resilient and efficient applications. Event-Driven Microservices - Beyond the Fairy Tale Our applications need to be faster, better, bigger, smarter, and more enjoyable to meet our demanding end-users needs. In recent years, the way we build, run, and operate our software has changed significantly. We use scalable platforms to deploy and manage our applications. Instead of big monolithic deployment applications, we now deploy small, functionally consistent components as microservices. Problem. Solved. Right? Unfortunately, for most of us, microservices, and especially their event-driven variants, do not deliver on the beautiful, fairy-tale-like promises that surround them.In this session, Allard will share a different take on microservices. We will see that not much has changed in how we build software, which is why so many “microservices projects” fail nowadays. What lessons can we learn from concepts like DDD, CQRS, and Event Sourcing to help manage the complexity of our systems? He will also show how message-driven communication allows us to focus on finding the boundaries of functionally cohesive components, which we can evolve into microservices should the need arise.
| Coven of Wisdom - Utrecht
Go to page for Mastering Event-Driven Design