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

Sometimes, we want to assign only if condition is true in ternary operator in JavaScript.

In this article, we’ll look at how to assign only if condition is true in ternary operator in JavaScript.

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

To assign only if condition is true in ternary operator in JavaScript, we use an if statement.

For instance, we write

if (max < b) {
  max = b;
}

to check if max is less than b.

If it is, then we assign b to max.

Conclusion

To assign only if condition is true in ternary operator in JavaScript, we use an if statement.