Javascript in 15 Days : Conditionals

March 4th, 2020

Welcome back!

Hi everyone! Glad to be back! Sorry, I had stopped posting for a while, but I’m back now! I’m sure you’re very excited about today’s topic! We will be talking about conditionals, which is as easy as ABC! But before we continue, if you haven’t read my last post, feel free to click on the link below!

Javascript in 15 Days : Variables & Data Types

Introduction

First off, I believe some of you already know what conditionals are, but some of us just learning now might not know, so let us have a brief intro to what conditionals are!

For example, when you are buying a piece of clothing, what comes to your mind? Some of us might think about the price, the color, the size or even maybe fabric type! These few things are very abstract, right? But in programming languages, we can use conditionals for unique purposes.

Let’s say, you want to buy a leather jacket under the following conditions:

  • Brand: Saint Laurent
  • Size: Medium
  • Color: Black
  • Price: Under US$70

Now, we know that what we need is very specific, so we could use conditionals to filter out those that we don’t need when searching in a marketplace. Like for example if the leather jacket’s price is under US$70, its color is black, the size is Medium and the brand is Saint Laurent please show in the results. This is just one of many uses of conditionals.

Another is if you are a teacher, and you want to find all-male student scores, then you can just search if gender is male, then show in result, other than that, hide it. Now that you get a grasp on the context, let’s dive into the Javascript world!

Example

Now, for the fun part, let’s say we have two numbers and we want to show/print out the higher number, we can use the below code!

var firstNumber = 21;
var secondNumber = 19;

if (firstNumber > secondNumber) {
    console.log('first number is the largest number'); // this will most likely be shown
} else if (secondNumber > firstNumber) {
    console.log('second number is the largest number');
} else {
    console.log('two numbers are of equal values');
}

As we can see, if we run this script in the terminal, it will print “the first number is the largest number” simply because 21 is larger than 19! Another way of solving similar problems is by using ternary operators. What are those?? Well to put it simply, it does the same purpose as normal if statements but makes our code cleaner and neater! For example, if you wanted to check if a person is considered legal or not (based on Canada) to drink or drive, you might write something similar to below…

var age = 23;

age >= 19
    ? console.log(
            'you are of legal age to drink and drive, though not simultaneously.'
      ) // this will be shown
    : console.log('you cannot drive or drink, as you are underage!');

So before the “?” we write our conditions (no parenthesis needed) and after that comes the command we want to be executed if it returns true, proceeded with a “:” and another command we want to execute if otherwise. The only downside of using ternary operators, if you haven’t noticed already is that it is not recommended to use it if the problem contains one or more “else if” statements, as ternary operators do not support such format, so you must use multilayered ternary operator statement, which decreases its readability. But other than that, you’re free to use it!

Summary

Here we are, at the end of today’s session! I hope you learned something today and maybe share some feedback on what you think is less understandable. I’ll see you soon and thanks for spending time to read!

Javascript in 15 Days : Switch Case