JavaScript basics if...else statement

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.
When you know the basics, you become so powerful! 💪 That's why this post is great!
Indeed, it's very good going back to the basics from time to time
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...

I sometimes forget that I write about content in a medium to advanced kind of matter.
In these JavaScript basics series, I'll be looking at some more fundamental topics of JavaScript, so you get a good idea of how to use these methods.
In this article, we'll be looking at using if...else statements in JavaScript.
An if statement can be used to execute code only when a specific condition is met.
Let's say we have a variable and want to evaluate whether it's true or false.
let our_var = true;
if(our_var === true) {
// Execute this code if true
console.log('Value is true);
}
In this case when we are checking boolean values, we don't need to specify the specific value so we can do this:
if(our_var) {
// Execute this code if true
console.log('Value is true);
}
You can also check if the value is false, like this:
if(!our_var) {
// Execute this code if false
console.log('Value is false);
}
We can even write this as a one-liner, but most linters will want to see the brackets for neatness.
if (our_var) console.log('Value is true');
Often you also want to execute some code if the first condition is not met. We can achieve this by using the else statement as well.
if (our_var) {
console.log('Condition is met');
} else {
console.log('Condition is not met, fallback?');
}
And you can even bind another if statement to this else, making it super powerful.
Let's say you want to check multiple conditions after each other.
if (our_var) {
console.log('first condition met');
} else if (our_second_var) {
console.log('Second condition was met!');
} else {
console.log('No condition was met');
}
You can make these as big as you want, but often you might want to consider using other solutions for bigger statements.
The JavaScript ternary operator is a quick way to express conditions and is often used for shorthand if...else.
The Syntax looks like this:
condition ? truthy : falsy;
If we take our example, we could write code like this:
our_var ? console.log('Condition is met') : console.log('Condition not met');
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter