How can I use an inline if
statement in JavaScript? Is there an inline else
statement too?
Something like this:
var a = 2; var b = 3; if(a < b) { // do something }
Method 1: In this method we write an inline IF statement Without else, only by using the statement given below. Method 2: In this method, we will use ternary operator to write inline if statement. Syntax: result = condition ?
It is commonly referred to as the conditional operator, inline if (iif), or ternary if. An expression a ? b : c evaluates to b if the value of a is true, and otherwise to c . One can read it aloud as "if a then b otherwise c".
In the logical AND ( && ) operator, if both conditions are true , then the if block will be executed. If one or both of the conditions are false , then the else block will be executed.
An inline function is a javascript function, which is assigned to a variable created at runtime. You can difference Inline Functions easily with Anonymous since an inline function is assigned to a variable and can be easily reused.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With