JavaScript Conditional Statements

1. if Statement


<!DOCTYPE html>
<html>
<body>
    <h2>if Statement</h2>
    <script>                        
        let age = 18;
        if (age >= 18) {
            console.log("You are an adult.");
        } else {
            console.log("You are a minor.");
        }
    </script>
</body>
</html>                          
                        

2. if...else Statement


<!DOCTYPE html>
<html>
<body>
    <h2> if...else Statement</h2>
    <script>                         
        let score = 75;
        if (score >= 50) {
            console.log("You passed the exam.");
        } else {
            console.log("You failed the exam.");
        }
    </script>
</body>
</html>                        
                        

3. if...else if...else Statement


<!DOCTYPE html>
<html>
<body>
    <h2> if...else if...else Statement</h2>
    <script>                          
        let marks = 85;
        if (marks >= 90) {
            console.log("Grade: A");
        } else if (marks >= 75) {
            console.log("Grade: B");
        } else {
            console.log("Grade: C");
        }
    </script>
</body>
</html>                         
                        

4. switch Statement


<!DOCTYPE html>
<html>
<body>
    <h2> switch Statement</h2>
    <script>                          
        let day = 3;
        switch (day) {
            case 1:
                console.log("Monday");
                break;
            case 2:
                console.log("Tuesday");
                break;
            case 3:
                console.log("Wednesday");
                break;
            default:
                console.log("Not a valid day.");
    }
    </script>
</body>
</html>
                        

5. Ternary Operator


<!DOCTYPE html>
<html>
<body>
    <h2> Ternary Operator</h2>
    <script>                        
        let isRaining = false;
        let message = isRaining ? "Take an umbrella." : "No need for an umbrella.";
        console.log(message); // Output: "No need for an umbrella."
    </script>
</body>
</html>   
                        

6. Short-Circuit Evaluation


                        
<!DOCTYPE html>
<html>
<body>
    <h2> Short-Circuit Evaluation</h2>
    <script>                          
        let x = 5;
        let result = (x > 0) && (x < 10);
        console.log(result); // Output: true
    </script>
</body>
</html>