if else in javascript

JavaScript Conditions includes if else statements and Switch case. Conditions are used to check logical conditions to control the flow of programme.

If is the the primary Condition used to control flow of programme. else and else if are optional.

Forms of If statement

  1. if
  2. if...else
  3. if...else if...else
  4. if...if


IF Statement

JavaScript if statement is used to check whether the given condition is true or not. We can use comparison operators to check condition. The code block inside if condition or statement will work only if the condition is matched. For exp

If Statement Code


  if(condition){
        statement
  }

  if(condition==true){
        statement
  }

  if(condition==true)
      statement

If Condition is very useful to check whether a number is greater than or equal to another number or not. we will use < (less than) operator. See example below.


const age=12;

if(age<18){
  alert("sorry, you are not eligible");
}

alert will show as condition is matched

If following condition doesn't match, code block inside if will not work. See example below


const age=20;

if(age<18){
  alert("sorry, you are not eligible");
}

alert will not show as condition is not matched

Feature Detection using if condition

JS If Condition can also be used to check browser features and then use them. This can avoid runtime errors on non supported browsers.

In example below we are checking device battery level and remaining time using JS Battery API.


if(navigator.getBattery){
    navigator.getBattery().then(function(x){ console.log(x.level* 100 + "%")});
    navigator.getBattery().then(function(x){ console.log(x.dischargingTime/3600+ " hours remaining")});
}
else{
    console.log("Battery Api Not Supported")
}

Check null or undefined

if can also check the variables is defined or not. Defined means a variable is neither nul, nor undefined.

3



  const t=3;
  if(t){
    console.log(t);   // defined
  }


  
  const t=undefined;

  if(t){
    console.log(t);  // no output
  }


    
const t=null;

if(t){
  console.log(t);   // no output
}


Else Statement

Else Statement is an extra block of code used with if condition. In any case if condition fails, code block inside else will run.


if(condition){
      code to run if condition is true
}
else{
      code to run if condition is false
}

Else statement can't be used without if statement. If if statement fails, then only else code works. See example below


const age=20;
      
if(age<18){
  alert("sorry, you are not eligible")
}
else{
  alert("Congrats, you are eligible")
}


Else if Statement

else if statement can be used after if statement only. Else if works in parallel with if statement. If if condition fails, then only else if statement will be checked.


if(condition){
      code to run if condition is true
}
else if(condition){
      code to run if else-if condition is true
}
else{
      code to run if both conditions are false
}

Check Positive number using if and else


const x=20;
      
if(x==0){
  alert("its zero")
}
else if( x>0){
  alert("Number is positive")
}
else{
  alert("Number is negative")
}

Nested if else

If statement can also be used inside another if or else statement. Nested if is used code will execute if first and nested both conditions are meet.


  if( statement 1 ){
    if( statement 2){
      console.log("both 1 and 2 matched");
    }
    else{
      console.log("only 1 matched");
    }
  }
  else{
    console.log("not matched");
  }

Check Number is even or odd

To check a number is even or odd in javascript, first we need to check if it is a number or not. We can check even or odd only if input is a number. See example below.


    const x=3;

    if( isNaN(x)){
      console.log("not a number");
    }
    else{
      if(x%2==0){
        console.log("even");
      }
      else{
        console.log("odd");
      }
    }