DEV Community

Vidya
Vidya

Posted on

Examples of If conditions in JavaScript

Example 1: Using else if

           let color="blue";

          if(color === "red"){
            console.log("stop!");
          } else if (color === "yellow"){
            console.log("Ready!");
          } else if (color === "green"){
            console.log("Go!");
          } else {
            console.log("unknown color");
          }
Enter fullscreen mode Exit fullscreen mode

Example 2: Nested if condition

          let user ="admin";
          let password ="1234";

           if(user === "admin"){
             if(password === "1234"){
               console.log("login successful!");
             } else {
               console.log("Wrong password");
             }
           } else {
             console.log("user not found");
Enter fullscreen mode Exit fullscreen mode

Try Yourself

  1.Write a program that checks if a number is positive,negative, or zero.
  2. Check student marks:
     If marks>=35
        If marks>=90 --> "Excellent"
        Else --> "pass"
     Else --> "Fail"
Enter fullscreen mode Exit fullscreen mode

Top comments (0)