How to assign only if condition is true in ternary operator in JavaScript

Forums JavaScriptHow to assign only if condition is true in ternary operator in JavaScript
Staff asked 2 years ago

Is it possible to do something like this in JavaScript?

max = (max < b) ? b;

In other words, assign value only if the condition is true. If the condition is false, do nothing (no assignment). Is this possible?

Answers (1)

Add Answer
Staff answered 2 years ago

You can use a logical && operator.

Ex: max = ( max < b )  &&  b

Subscribe

Select Categories