Friday, October 24, 2025
HomeLanguagesJavascriptHow to write an inline IF statement in JavaScript ?

How to write an inline IF statement in JavaScript ?

We can write an inline IF statement in javascript using the methods described below.
Method 1: In this method we write an inline IF statement Without else, only by using the statement given below.

Syntax:

(a < b) && (your code here)

Above statement is equivalent to
if(a < b){
   // Your code here
}

Example: Below is the implementation of above approach:




<script>
    // Javascript script 
    // to write an inline IF
    // statement
      
    // Function using inline 'if'
    // statement to print maximum
    // number
    function max(n, m){
          
        // Inline 'if' statement only
        // If n > m then this will execute
        (n > m) && document.write(n + "<br>");
        // Above statement is equivalent to
        // if(n > m){
        //    document.write(n + "<br>");
        // }
          
        // Inline 'if' statement only
        // If m > n then this will execute
        (m > n) && document.write(m + "<br>");
        // Above statement is equivalent to
        // if(m > n){
        //    document.write(m + "<br>");
        // }
    }
      
      
    //Driver code
    var a = -10;
    var b = 5;
      
    // Call function
    max(a, b);
      
    // Update value
    a = 50;
    b = 20;
      
    // Call function
    max(a, b);
</script>


Output:

5
50

Method 2: In this method, we will use ternary operator to write inline if statement.

Syntax:

result = condition ? value1 : value2;

If condition is true then value1 will be assigned to result variable and if wrong then value2 will be assigned.

Example: Below is the implementation of above approach:




<script>
    // Javascript script 
    // to write an inline IF
    // statement
      
    // Function using inline 'if'
    // statement to return maximum
    // number
    function max(n, m){
          
        // Inline 'if' statement
        // using ternary operator
        var x = (n > m) ? n : m;
        // Above statement is equivalent to
        // if(n > m){
        //    x = n;
        // }
        // else {
        //    x = m;     
        // }
          
        return x;
    }
      
    //Driver code
    var a = -10;
    var b = 5;
    var res;
      
    // Call function
    res = max(a, b);
    // Print result
    document.write(res + "<br>");
      
    // Update value
    a = 50;
    b = 20;
      
    // Call function
    res = max(a, b);
    // Print result
    document.write(res + "<br>");
</script>                    


Output:

5
50
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS