Conditionals In Javascript

Conditionals In Javascript

Introduction

  • Conditional statements are used for making decisions based on different conditions.

  • Conditionals in JavaScript allow you to execute different blocks of code based on whether a certain condition is true or false.

Conditions can be implemented using the following ways:-

1) If

2) If Else

3) Else If

4) Switch

5) ternary operators


1) If statement:-

  • Shows truthy values

  • used to check if the given conditions are true & then execute the block of code.

      if(condition){
      -----block of code which is going to execute-----
      }
    

Let's understand this with an example.

let num = 10
if (num > 0) {
console.log(num + "is a positive number")
} 
//Output => 10 is a positive number//
//In the above example, we set the condition that if the user enters any number greater than 0, then "if" condition got executed and it returns the output.//

2) Else statement:-

  • It's like the opposite of an 'If' statement.

  • So we will say that, if the 'If' condition is not executed which will happen when the given condition is false, then the 'Else' statement gets executed.

if(condition){
-----block of code which is going to execute-----
} else {
-----block of code which is going to execute-----
}

Let's take an example and try to understand this.

//lets say we made a google form for football trails and age limit for the peoples who can apply for this is 16+. Now, If the user enter age more or less than the given age, certain blocks of code gets executed and give response accordingly.//
let myAge = 15
if (myAge > 16) {
console.log(myAge + " is valid age, you are eligible for trials.")
} else {
console.log(myAge + " is not a valid age, you are not eligible for the trials .")
}
//I Hope this clears how "If" and "Else" statements works//

3) Else If:-

  • This is used for most cases because there are multiple options to select from sometimes.

      if(condition){
      -----block of code which is going to execute-----
      } else if(condition){
      -----block of code which is going to execute-----
      } else {
      -----block of code which is going to execute-----
      }
    

    Let's understand this with an example.

  • Let's say, we found an interesting website and in order to get the most out of this website it's asking us to make an account on it. As we are going through the process of making an account, it asks us to set questions and answers, in case we lost our password we can still be able to log in by giving the correct answer to the questions. Now, a few months pass and we want to sign in to that website but we couldn't remember our password, so the website gives us the option to sign in by giving answers to the questions we set previously. It gives us a question and four options to choose from.

Que) what is your favorite color?

a) blue

b) Indigo

c) pink

d) red

let favColor = 'blue'
if(favColor === 'indigo'){
console.log("indigo is not your favorite color.Try again") 
} else if(favColor === 'pink'){
console.log("pink is not your favorite color.Try again")
} else if(favColor === 'red'){
console.log("Seriously, red, broooo Try again")
} else if(favColor === 'blue'){
console.log("Thats my bro, blue is your fav color. Never forget your password again.")
} else {
console.log("Enter your fav color")
}

4) Switch statement:-

  • 'Switch statement' is an alternative for 'If Else' statements.

  • The 'Switch statement' makes the code more concise and easier to read when you need to test a single variable against multiple possible values.

      switch (case value) {
      case 1: 
         console.log('   ')
         break; //Suppose, the condition is satisfied after end of case 1, then, 'break' terminates the code//
      case 2:
         console.log('   ')
         break;
    
      default: //default runs only if all the cases dont satisfy conditions.//
         console.log(' ')
      }
    

    Let's understand this with an example.

let theDay = 'tuesday'
switch(theDay) {
case'monday':
   console.log('Today is not monday');
   break;
case'tuesday':
   console.log('Yes, today is tuesday');
   break;
default:
console.log('Please enter a valid day');
}
//In this example, the code terminates after 2nd case, as the condition is satisfied at case 2//

5) Ternary operator:-

  • It is a simple way of writing an 'If Else' statement.

It takes three values or operands

  • A condition

  • expression to execute if the condition is true

  • expression to execute if the condition is false

Let's understand this with an example.

let playFootball = true
playFootball
? console.log('you needs football boots to play on ground')
: console.log('i dont wanna play')
  • The ternary operator is useful when you need to make a simple decision based on a single condition. It can make your code more concise and easier to read, especially when used with short expressions.

Conclusion

This blog discusses various conditional statements in JavaScript, which allow the execution of different blocks of code based on certain conditions. This includes If, Else, Else If, Switch, and Ternary operators. If statements are used to check whether a condition is true, while Else statements execute when the If condition is false. Else If statements are used when multiple options need to be considered. Switch statements are an alternative to If Else statements and make the code more concise. Ternary operators are a simple way of writing If Else statements.

Did you find this article valuable?

Support Shashank Sharma by becoming a sponsor. Any amount is appreciated!