How to write an inline IF statement in JavaScript? How to write an inline IF statement in JavaScript? javascript javascript

How to write an inline IF statement in JavaScript?


You don't necessarily need jQuery. JavaScript alone will do this.

var a = 2;var b = 3;    var c = ((a < b) ? 'minor' : 'major');

The c variable will be minor if the value is true, and major if the value is false.


This is known as a Conditional (ternary) Operator.

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Conditional_Operator


There is a ternary operator, like this:

var c = (a < b) ? "a is less than b"  : "a is not less than b";


For writing if statement inline, the code inside of it should only be one statement:

if ( a < b ) // code to be executed without curly braces;