javaScript if else - conditional statements

the if statement is javascript's basic decision-making control statement.

javascript conditional statements are used to control the execution of statements based on the conditions.

 javascript provides the four different type of conditional statements.such as

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

javaScript if statement

the if statement is straight forward if the given condition is true, the statements within braces will be executed.otherwise, they are skipped.

syntax :

                if(condition){
                           
                                 first_statement__1 ;
                                                                     // block code only run if condition is true
                                 second_statement__2 ;  
                                                                               
                           }

in the above if statement condition is evaluated to a boolean, and, if the condition is true, the statement is executed.otherwise the control skip the if evaluated block.

javascript if statement condition can be any expression that evaluates to true or false.

   <script>
         
             function    result(){
                 
                 if( 5<6 &&  6===6 ){   document.write("this is the true block"); }
                      
                 if(false){    document.write("this is the false block"); }
                    
                    }
                    
    </script>

javascript nested if statements :- You can have 'nested' if statements within a single if statement, using additional parenthesis.

    <head>
    
        <script>
        
                  var    a = 4 ;  var b = 44 ; var c = 55 ;
                  
                  if(a==a && a < b ) {
                  
                                 if(b < c ||  a < c ){
                                 
                                        alert ("the javascript nested block run");
                                        
                                       }else{  document.write("nested if condition false");}
                                       
                                     document.write("outer if statement block code");       
                                  }
     </script></head>

Javascript if  else statement

if condition is true then if evaluates block is executed otherwise the else block execute.

the else clause is optional.

syntax:

                if(condition){
                      
                                                // if block code run  only if  condition is true
                                   
                              }else {
                                     
                                            // else block code run when  the condition is false
                                        
                                    }
     <script>
         
             function    result(){
                 
                 if( 5<6 &&  7===6 ){   
                  
                                             document.write("this is the true block");
                                                           
                                    }else{    
                                    
        document.write("this is else evaluated block because if condition false"); 
                                                  
                                          }}
                    
    </script>

javaScript if  else if  else statements

the javascript if  else if  else statement use to multiple conditions tested in sequence.

syntax:

                if(condition_1){
                      
                                                      // if block run only if the condition_1 is true
                                   
                              }
                
                else if (condition_2){              
                
                                               // else if block run only if the condition_2 is true
                                                
                               }else {
                                     
                                           // else block run if the all conditions are false
                                        
                                    }
     <script>
         
             function    result(){
                 
                 if( 5<4 &&  6===6 ){   
                  
                                                 document.write("this is if block");
                                                           
                                           }
                                    
                 else if( 4==4 && 6>=6){
                 
                                                document.write("this is the else if block");
                                             
                                       }else{    
                                    
                                             document.write("this is the else block"); 
                                                  
                                          }
                                  }
                    
    </script>

in javascript conditional statements, the following values will evaluate to false:

  • false
  • 0
  • nan
  • null
  • undefined
  • empty string("")

example

      if(""){document.write("this is true block"); }

do not confuse the primitive boolean values true and false, with the true and false values of the boolean object.

 var    f = false ;

                      if(f)           // this condition evaluates to false
 var    f = new  boolean(false);

                      if(f)      // this condition evaluates to true