# Conditionals in JS

The conditional statements in any language are the statements which are used to give our program a way to choose between multiple choices or choose a path based on certain variables.

Java script also offers conditional statements. There are two types of conditional statements in Java script.

* * *

### If-else statements

If-else statements in java scripts allows us to direct the flow of our program based on certain conditions. The condition specified in if-else block should return a Boolean result. The syntax of if-else statement in java script are as follows:

```javascript
if (condition) {
    //task to do if condition is true
} else{
    //task if condition turns to be false
}
```

We can use if statement with else block as well. It can be used to perform certain tasks only when a condition is true otherwise skipping them.

```javascript
// previous code...

if (condition){
    // Code to run only when the condition is true.
}

// rest of the code...
```

Some example use cases of if-else statements in java script are shown below:

```javascript
// Checking if a number is positive or not

let num = 4;

if (num >= 0) {
    console.log("The number is positive");
} else {
    console.log("The number is negative");
}

// Output : The number is positive
```

This code snippet checks for the num variable to be positive or negative and prints it on the console.

```javascript
let num = -2;

if (num < 0) {
    num = num * -1
}

console.log("The number is:", num);

// Output : The number is: 2
```

The code snippet checks the number of to be negative or not, if its a negative number it multiplies with "-1" to make it positive. Then prints it on the console.

**Note:** Writing `else block` with `if` is not always necessary.

<h4>Nested `if-else-if` statements</h4>

Yes! we can use another if-else block or if statement inside else block of another if-else block. The syntax for that is:

```javascript
if (condition_1) {
    // code to run if condition 1 is true.
} else if (condition_2) {
    // code to run if condition 2 is true.
} else if (condition_3) {
. 
.
.
} else {
    // code to run if no condition is true.
}
```

**Note:** Defining a else block is not necessary here also.

Example,

```javascript
let num = 0;

if (num > 0) {
    console.log("The number is positive.");
} else if (num < 0) {
    console.log("The number is negative.");
} else {
    console.log("The number is zero.");
}

// Output : The number is zero.
```

The program uses `if-else-if` statement to checks for the sign of a number and outputs it on console.

* * *

### Switch Statements

Switch statements are the second type of conditional statements offered by java script. They are used to compare a variable in our program with multiple possible values.

The syntax for using switch statements are as follows:

```javascript
switch (variable) {
    case (value_1):
        // code to run if variable has value 1.
        break;
    case (value_2):
        // code to run if variable has value 2.
        break;
    .
    .
    .
    default:
        // code to run if no value matches.
}
```

The switch statements uses two more keywords also to complete the statement completely. They are:

1.  `case` : It is used to define a possible value for the variable or expression which is being checked in switch statement.
    
2.  `default` : It is used to define the default set of code that should run if the variable or expression in switch statement does not match any values defined in the cases.
    

If you have noticed we are using `break` after defining every case. Every case if being defined like this because of how the switch statements works in java script.

Whenever, one of the cases match the expression defined in the switch statement, the code inside that respective case block runs as expected but if the `break` is not used inside that case block then all the case blocks and default block runs as well until a `break` is not encountered. **This is why writing a break in every case block is necessary.**

A example code showing the use of switch statement is shown below:

```javascript
let dayOfTheWeek = 4;

switch (dayOfTheWeek) {
    case 1:
        console.log("Its Monday");
        break;
    case 2:
        console.log("Its Tuesday");
        break;
    case 3:
        console.log("Its Wednesday");
        break;
    case 4:
        console.log("Its Thursday");
        break;
    case 5:
        console.log("Its Friday");
        break;
    case 6:
        console.log("Its Saturday");
        break;
    case 7:
        console.log("Its Sunday");
        break;
    default:
        console.log("Its not a valid day of the week");

// Output : Its Thursday
```

The above code snippet displays the day of the week as per the given number and shows a fallback message if the number is not within the range of 7.

After reading the article, I hope that you will try writing a switch statement and checks what will be the output of the above snippet if we don't use the breaks inside the case block.

## Conclusion

We have discussed the two different types of conditional statements present in the javascript alongside their use cases and some examples as well. I look forward to see the outputs of the assignment that i gave you in the comment section.
