If-Else in JavaScript

If-else statements are fundamental building blocks for decision-making in JavaScript programs. They allow you to control the flow of your code based on whether a certain condition is True or False.

  • Structure of If-Else :
  • The basic syntax of an if-else statement is:
    if (condition) {
    // code to execute if the condition is True
    }
    else (condition) {
    // code to execute if the condition is False
    }

    The if keyword is followed by a condition that is evaluated as True or False.

    If the condition is True, the indented code block after the if statement is executed.

    If the condition is False, the indented code block after the else statement (if present) is executed.

    JavaScript code
    # Example : Checking for Even/Odd. <!DOCTYPE html> <html> <head> <title></title> </head> <body> <script type="text/javascript"> var n=parseInt(prompt("Enter a number : ")); if(n%2==0) { document.write("Even"); } else { document.write("Odd"); } </script> </body> </html>

    This program asks the user for a number. It then uses an if statement to check if the number is divisible by 2. If the remainder is 0, then the number is Even, otherwise it's Odd.

  • Key Points :
  • The condition can be any expression that evaluates to True or False. Common comparisons include == (equal to), != (not equal to), < (less than),> (greater than), <= (less than or equal to), and>= (greater than or equal to).

    You can have multiple lines of code indented within the if and else blocks

    If the condition is False, and there's no else block, no code is executed after the if statement.

  • Basic Examples
  • JavaScript code
    1. Write a program to accept a number and check whether the number is a perfect square or not. A number is known as a square number or perfect square if the number is a square of another number. Example: 9 = 3 * 3
    <!DOCTYPE html> <html> <head> <title></title> </head> <body> <script type="text/javascript"> n=parseInt(prompt("Enter the no")); a=Math.sqrt(n); if(a*a==n) document.write("Perfect square"); else document.write("Not Perfect square"); </script> </body> </html>

    Output :
    Enter the no : 9
    Perfect square
    Enter the no : 6
    Not Perfect square

    JavaScript code
    2. Write a program to accept three numbers and check whether they are Pythagorean Triplet or not. A "Pythagorean Triple" is a set of positive integers, a, b and c that fits the rule:𝑎2 + 𝑏2 = 𝑐2
    <!DOCTYPE html> <html> <head> <title></title> </head> <body> <script type="text/javascript"> var a=parseInt(prompt("Enter value of a : ")); var b=parseInt(prompt("Enter value of b : ")); var c=parseInt(prompt("Enter value of c : ")); if(a*a+b*b==c*c) { document.write("Pythagorean Triplet"); } else { document.write("Not a Pythagorean Triplet"); } </script> </body> </html>

    Output :
    Enter value of a : 3
    Enter value of b : 4
    Enter value of c : 5
    Pythagorean Triplet
    Enter value of a : 5
    Enter value of b : 6
    Enter value of c : 7
    Not a Pythagorean Triplet

    JavaScript code
    3. Write a program to check whether a Number is Positive , Negative or Zero.
    <!DOCTYPE html> <html> <head> <title></title> </head> <body> <script type="text/javascript"> var a=parseInt(prompt("Enter a number : ")); if(a>0) { document.write("Positive"); } else if (a<0) { document.write("Negative"); } else { document.write("Zero"); } </script> </body> </html>

    Output :
    Enter a number : 5
    Positive
    Enter a number : -5
    Negative

    The elif statements are checked only if the previous if conditions are False.

  • Practice Exercises:
  • 1. Write a program to find largest of three numbers.

    2. Write a program to accept a number and check whether the number is Buzz number or not. A number is said to be Buzz Number if it ends with 7 or is divisible by 7. Example - 1007