Conditions can be implemented using the following ways:-

  1. If
  2. If Else
  3. Else If
  4. Switch
  5. ternary operators

1) If statement:-

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:-

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 secret questions and document their answers. So, 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:-

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 takes three values or operands:

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.


Also published here.