How to write an inline IF statement in JavaScript ? Last Updated : 29 May, 2019 Comments Improve Suggest changes Like Article Like Report 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: javascript <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: javascript <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 Comment More infoAdvertise with us Next Article How to write an inline IF statement in JavaScript ? R Rajnis09 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to Use AND Statement in if with JavaScript? In JavaScript AND (&&) logical operator is used in the 'if' statement to check for two or more conditional validities. AND logical operator in the 'if' condition returns true only when all the conditions inside the 'if' statement are TRUE. If any one condition inside the 'if' statement is FA 2 min read How to Set Multiple Conditions in an If Statement in JavaScript? In JavaScript, conditional statements like if are used to execute code blocks based on certain conditions. There are situations where we might want to check multiple conditions within a single if statement. In such cases, JavaScript provides logical operators like && (AND) and || (OR) to com 5 min read What does OR Operator || in a Statement in JavaScript ? JavaScript is a dynamic programming language that allows developers to write complex code with ease. One of the fundamental concepts in JavaScript is the use of operators, which are symbols that perform operations on one or more values. One such operator is the || (logical OR) operator, which can be 6 min read How to check if a Variable Is Not Null in JavaScript ? In JavaScript, checking if a variable is not null ensures that the variable has been assigned a value and is not empty or uninitialized. This check is important for avoiding errors when accessing or manipulating data, ensuring that the variable holds valid, usable content before proceeding with oper 4 min read How to write a function in JavaScript ? JavaScript functions serve as reusable blocks of code that can be called from anywhere within your application. They eliminate the need to repeat the same code, promoting code reusability and modularity. By breaking down a large program into smaller, manageable functions, programmers can enhance cod 4 min read How to optimize the switch statement in JavaScript ? The switch statement is necessary for certain programming tasks and the functionality of the switch statement is the same among all programming languages. Basically switch statements switch the cases as per the desired condition given to the switch. A switch statement can be used when multiple numbe 2 min read Control Statements in JavaScript JavaScript control statement is used to control the execution of a program based on a specific condition. If the condition meets then a particular block of action will be executed otherwise it will execute another block of action that satisfies that particular condition.Types of Control Statements i 4 min read What is the inline function in JavaScript ? In JavaScript, an inline function is a special type of anonymous function that is assigned to a variable, or in other words, an anonymous function with a name. JavaScript does not support the traditional concept of inline functions like C or C++. Thus anonymous function and inline function is practi 2 min read JavaScript switch Statement The JavaScript switch statement evaluates an expression and executes a block of code based on matching cases. It provides an alternative to long if-else chains, improving readability and maintainability, especially when handling multiple conditional branches.Switch Statement Example: Here, we will p 5 min read JavaScript - Conditional Statements JavaScript conditional statements allow you to execute specific blocks of code based on conditions. If the condition is met, a particular block of code will run; otherwise, another block of code will execute based on the condition.1. Using if StatementThe if statement is used to evaluate a particula 4 min read Like