JavaScript basics logical operators

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

In today's article, we'll be looking at JavaScript logical operators.
JavaScript comes with three logical operators being and, or and not.
Check out the below table of the basic use cases.
| Operator | Logic | Example | ||||
&& | And | a = true && b = false | ||||
| `\ | \ | ` | Or | `a = true \ | \ | b = false` |
! | Not | let a = true!a // false |
Let's have a more detailed view of each of these logical operators in JavaScript.
The and operator can be used to assess if two expressions are met.
The syntax is as follows:
expression && expression;
Some examples might be:
const a = true;
const b = 5;
a === true && b > 3;
// true
The return will always be an evaluation in the form of a boolean. We are returning either true if both expressions are met or false when one or both fails.
The operator is often used with a if...else statement to perform an action based on the logic.
Much like the and operator, we can also use the or operator, which is used by placing two pipes like this: ||.
This operator is used to evaluate if both or one of the expressions is met.
expression || expression;
Let's say we want to check if a is true or b is greater than 3. We don't need both to be truthy, just one.
const a = true;
const b = 1;
a === true || b > 3;
// true
The above example will still return true since it will succeed to be correct.
This is a bit of a funny one, as it is used to invert the value of a boolean.
So let's say we have a true boolean and want to convert it to false:
let a = true;
!a;
// false
However, using this in an if statement will evaluate if the condition is NOT met.
let a = true;
if (!a) {
// It will never get here now
}
However, we mainly use this to convert a value to the opposite boolean value.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter