JS Tutorial

Function Reference

Method Reference

  • JS String
  • JS Array
  • JavaScript if..else

    JavaScript supports the following types of if..else statement

    • if statement
    • if...else statement
    • if...else if... statement

    JS if statement

    The if statement is the basic control statement that check the condition if condition is true then block of statement to be execute.

    Syntax

    if (condition) {
       Statement(s) to be executed if condition is true.
    }
    

    Example

    var check = 5;
    if(check>4)
    {
     document.write('eligible for gratuity');
    }
    //Output
    //eligible for gratuity
    

    JS if...else statement

    The if...else statement(s) executes some code if a condition is true and another code if that condition is false.

    Syntax

    if (condition) {
       Statement(s) to be executed if condition is true.
    } else{
    	Statement(s) to be executed if condition is false.
    }
    

    Example

    var check = 18;
    if(check>17)
    {
     document.write('eligible for vote');
    } else{
     document.write('Not eligible for vote');
    }
    //Output
    //if var check value is 18 then eligible for vote
    //if var check value is 18 then Not eligible for vote
    

    JS if...elseif...else Statement

    The if...else if... statement used for more than two conditions to be checked

    Syntax

    if (condition) {
       Statement(s) to be executed if condition is true.
    else if (condition) {
       Statement(s) to be executed if condition is true.
    } else{
       Statement(s) to be executed if no condition is true.
    }