Conditional statements in Js

Conditional Statement in JavaScript



Conditional statements are used to perform different actions based on different conditions.

Conditional Statements

Very often when you write code, you want to perform different actions for different decisions.

You can use conditional statements in your code to do this.

In JavaScript we have the following conditional statements:

  • Use if to specify a block of code to be executed, if a specified condition is true
  • Use else to specify a block of code to be executed, if the same condition is false
  • Use else if to specify a new condition to test, if the first condition is false
  • Use switch to specify many alternative blocks of code to be executed

if Condition

Example

        <script>

        a = 10;
        b = 20;

        if (a > b){
            document.write(" a is equal to b");
        }


    </script>


if - else Statement

Use the else statement to specify a block of code to be executed if the condition is false.

        <script>

        x = 15;
       
        if (x > b){
            document.write(" X is Greater");
        } else{
            document.write(" X is Smaller ")
        }

    </script>

Ternary operators

Ternary Operators , are also known as conditional operators. Basically, they put an if else statement onto one line.

        // if else statement
        var number = 99;
        var result

        if(number>=100){
            result='not to nine nine'
        }else{
            result = 'It could be large'
        }

        console.log(result)

Ternary Operators
        // if else statement
        var number = 99;
        var result

        // if(number>=100){
        //     result='not to nine nine'
        // }else{
        //     result = 'It could be large'
        // }

        // ternary operators
        result = number >= 100 ? "Not to nine nine" : "It could be large"
        console.log(result)
       

if - else if

The if a statement executes a statement if a specified condition is true. If the condition is false, another statement can be executed.

  • if statements: if a condition is true, it executes a specific block of code.
  • else statements: if the same condition is false, it executes a specific block of code.  else if: this specifies a new test if the first condition is false.

    var hour = 14;

            if(hour >= 6 && hour < 12){
                console.log("Good Morning");
            }else if (hour >= 12 && hour < 18){
                console.log("Good Agternoon");
            }else{
                console.log("Good Evening")
            }

Switch
 Statement

The switch statement is used to perform different actions based on different conditions.

This is how it works:

  • The switch expression is evaluated once.
  • The value of the expression is compared with the values of each case.
  • If there is a match, the associated block of code is executed.
  • If there is no match, the default code block is executed.

Example :


        <script>

        var day = 5 ;

        switch (day){
            case 0:
            document.write("Today is Monday");
            break;
            case 1:
            document.write("Today is Tuesday");
            break;
            case 2:
            document.write("Today is Monday");
            break;
            case 3:
            document.write("Today is Monday");
            break;
            case 4:
            document.write("Today is Monday");
            break;
            case 5:
            document.write("Today is Monday");
            break;

            default:
            document.write("Enter the valid week day");
        }

    </script>

Loops in JavaScript

Loops are something that is with us always since the beginning of our never ending journey of learning JavaScript(or any programming language) so let's focus on them. 

What loops ?

Loop is divided into 3Parts
1) Initialization
2) condation ,
( condition is true you will get result OR condition is false,it will go out of loop )
3) increment / decrement

loops  are ?

*while loop

*do...While loop
*for
*for..in
*for...of

So first let’s define what is a loop and why we need to use them in JavaScript?

Do you know when you like a music so much that you just listening it over and over? Well that is a loop. For programming a loop is a way to iterate over something like an array or an object for example. With loops we make sure our code is DRY. The loop will loop till the condition is met or loop if the condition is met and till the condition is false. 
A infinite loop is loop that has not ending and most likely it will crash your app/website


While Loop

The while loop it runs as long the condition evaluated is true. Think of it as a conditional, an if statement but instead of running once, run as long the condition is true.

If you don’t write the condition or write a condition that will never turn false


    <script>
        var a = 1;
        while (a<= 10){
            document.write(" Code Guruva <br>");
            a = a + 1 ;
        }
    </script>
   


do...while Loop


The do…while works a bit different. The big difference is that the do… while loop runs at least once. Basically it says to do something while this condition is true running always at least once.

Now let's see a situation where it only run once:


    <script>

        var a = 1;

        do {
            document.write( a + " Code Guruva <br> ");
            a++; // a = a+1;
        }while(a<=9)

    </script>



for loop

One of the most used loops in JavaScript is the for loop. When I start coding with JS, this was one of the ones I used more till today. In the beginning I was not getting so much how it work so I try to explain in a way that I would understand when I started

In the for loop, the code is repeated until the condition evaluates to false. 
One of the uses of the for loop is to loop over an array.

Syntax:


        for([initialization];[condition]; [final-expression])
       

So when in use, would be looking something like this:


        for(let i =0; i <5 ; i++){
            //do something
        }


Example

    <script>

            for ( var a = 1; a <=10 ; a++){
                document.write("Code Guruva<br>");
            }
    </script>