Quick Contact

    JavaScript If…Else Statements

    Introduction

    JavaScript, like many other programming languages, allows you to build code that performs various actions at run time based on the outcomes of logical or comparative test conditions. This means you may build test conditions in the form of expressions that evaluate to true or false, and then conduct actions based on the findings.

    JavaScript Conditional Statements

    In JavaScript, you may make decisions using conditional statements such as:

    • The if statement
    • The if…else statement
    • The if…else if….else statement
    • The switch…case statement

    We will discuss each of these statements in detail in the coming sections.

    The if Statement

    The if statement executes a piece of code only if the stated condition is true. The following are the basic JavaScript conditional statements:

    Syntax

    if(condition) {
        // Code to be executed
    }
    

    Example

    < html>
    	< body>
    		< script>
    var x=30;  
    if(x>20){  
    document.write("value of x is greater than 20");  
    }  
    		< /script>
    	< /body>
    < /html>
    

    Output

    value of x is greater than 20

    The if…else Statement

    You may improve the decision-making skills of your JavaScript application by adding an else statement to the if statement.

    If the stated condition is true, the if…else statement allows you to run one block of code and another piece of code if it is false. It may be written as follows:

    Syntax

    if(condition) {
        // Code to be executed if condition is true
    } else {
        // Code to be executed if condition is false
    

    Example

    < html>
    	< body>
    		< script>
    var x=50;  
    if(x%2==0){  
    document.write("x is even number");  
    }  
    else{
    document.write("x is odd number");  
    }  
    		< /script>
    	< /body>
    < /html>
    

    Output

    x is even number

    The if…else if…else Statement

    The if…else if…else statement is an unique statement used to combine numerous if…else statements.

    Syntax

    if(condition1) {
        // Code to be executed if condition1 is true
    } else if(condition2) {
        // Code to be executed if the condition1 is false and condition2 is true
    } else {
        // Code to be executed if both condition1 and condition2 are false
    }
    

    Example

    < html>
    	< body>
    		< script>
    var x=20;  
    if(x==10){  
    document.write("x is equal to 10");  
    }  
    else if(x==15){  
    document.write("x is equal to 15");  
    }  
    else if(x==20){  
    document.write("x is equal to 20");  
    }  
    else{
    document.write("x is not equal to 10, 15 or 20");  
    }  
    		< /script>
    	< /body>
    < /html>
    

    Output

    The Ternary Operator

    The ternary operator is a shortcut for creating if…else expressions. The question mark (?) symbol represents the ternary operator, which accepts three operands: a condition to verify, a result for true, and a result for false. Its fundamental syntax is as follows:

    Syntax

    var result = (condition) ? value1 : value2

    If the condition is evaluated to true the value1 will be returned, otherwise value2 will be returned.

    Example

    < !DOCTYPE html>
    < html lang="en">
    < head>
    < meta charset="utf-8">
    < title>JavaScript Typical Conditional Statement< /title>
    < /head>
    < body>
    < script>
    varuserType;
    var age = 21;
    if(age < 18) {
    userType = 'Not Eligible for Voting';
        } else {
    userType = 'Eligible for Voting';
        }
    document.write(userType); // Prints Adult
    < /script>
    < /body>
    < /html>
    

    Output

    Eligible for Voting

    Copyright 1999- Ducat Creative, All rights reserved.

    Anda bisa mendapatkan server slot online resmi dan terpercaya tentu saja di sini. Sebagai salah satu provider yang menyediakan banyak pilihan permainan.