JavaScript if shorthand without the else

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.
Here you go: https://daily-dev-tips.com/posts/javascript-optional-chaining-to-the-rescue/
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...

At one point in your career, you'll find yourself using the ternary operator in JavaScript a lot.
It's a fancy word for the if...else shorthand version.
Let's recheck the example.
// Full if else
let result = '';
if (our_value) {
result = 'we got a value';
} else {
result = 'no value';
}
// Ternary
result = our_value ? 'we got a value' : 'no value';
So that's already a set working example.
However, sometimes we might want to execute or set something if a specific condition is met.
Let's look at the following example.
if (our_value) {
alert('I have the value');
}
This piece of code should alert the user if the our_value condition is truthy.
There is no else involved.
We could write it like this:
our_value ? alert('I have the value') : null;
However, we don't need the null value as it doesn't do anything, so we can switch to using the && operator.
our_value && alert('I have the value');
Way cleaner, we tell the script if we do have this truthy value, we should alert the user.
In some cases, you might have a function that returns some value, but you might want to return a default value if the object is nullish.
const name = null;
const user = {
username: name ? name : 'John Doe',
};
console.log(user);
// { username: 'John Doe' }
That example works perfectly fine. However, it's a bit duplicated again. We can leverage the ?? operator to set a default return value.
This ?? operator is called a logical nullish operator.
const user = {
username: name ?? 'John Doe',
};
This will either return the value of name, and if that doesn't exist, it will return John Doe.
Note: Be careful when the value can be
falseas it will return false in that case.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter