takeItEasy takeItEasy - 1 year ago 123
Javascript Question

How to write an inline IF statement in JavaScript?

How can I use an inline

statement in JavaScript? Is there an inline
statement too?

Something like this:

var a = 2;
var b = 3;

if(a < b) {
// do something

Answer Source

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

var a = 30;
var b = 40;    
var c = ((a < b) ? 2 : 3);

There 2 is the value when true, 3 is the value when false.

This is known as a Conditional (ternary) Operator.


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download