JavaScript Under The Hood : Narrowing With Logical Operators

JavaScript Under The Hood : Narrowing With Logical Operators

In this article, we will discuss how these two operators work under the hood, how/when they can be used to replace conditional statements, and go over their pitfalls.

|| (Logical OR) and && (Logical AND) are very common operators used in JavaScript. But do you know why they work the way they do and how else they can be used? Not only that, but are you aware of their pitfalls? Logical OR and Logical AND can actually cause hard-to-find bugs in your code if you’re unaware of how they work under the hood.

Logical OR and Logical AND can cause hard-to-find bugs in your code if you’re unaware of how they work under the hood.

Aside from their popular uses, Logical OR || and Logical AND &&can actually be used fairly easily and efficiently to replace simple conditional statements, and often can reduce them down to one line.

In this article, we will discuss how these two operators work under the hood, how/when they can be used to replace conditional statements, and go over their pitfalls.

How do Logical OR and Logical AND work under the hood?

First, if we take a look at Mozilla’s Operator Precedence table (scroll down a bit to get to the table), we can see that these both have left-to-right associativity (commonly referred to as “left associativity”), which in essence means the JavaScript engine reads whatever is on the left side of the operator first before whatever is on the right side. This will be important in the points to follow. (If you want to read a little more on operator associativity, check out this Wikipedia article)

Before we move forward, I’d like to give a bit of credit to Execute Program’s course on TypeScript. While this article isn’t about TypeScript, much of these concepts are explained during their course.

To understand how || and &&can create unforseen bugs, lets briefly go over falsey values in JavaScript.

programming software-engineering javascript software-development coding

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Software Developer vs Software Engineer — Differences: Bogus or Real?

In this article, see if there are any differences between software developers and software engineers. What you’re about to read mostly revolves around my personal thoughts, deductions, and offbeat imagination. If you have different sentiments, add them in the comment section, and let’s dispute! So, today’s topic…

Offshore Software Development - Best Practices

To make the most out of the benefits of offshore software development, you should understand the crucial factors that affect offshore development.

5 Core Criteria for Selecting Software Development Company - TopDevelopers.co

Check out these five criteria for the selection of your software vendor, and you will never regret having the wrong quality product made for you.

Wrestling Betting Software Development | WWE Betting Software Developers

Wrestling betting software development solutions from expert Wrestling betting app and software development company delivers ultimate betting experience. Chat with us.

Programming quotes to help you become a better Software Developer

Once in a while I also create posts like this: Not purely technical but having enough technical relevance that it is worth sharing within the tech community.