Assign only if condition is true in ternary operator in JavaScript -



Assign only if condition is true in ternary operator in JavaScript -

is possible in javascript?

max = (max < b) ? b;

in other words, assign value if status true. if status false, nil (no assignment). possible?

don't utilize ternary operator then, requires 3rd argument. need reassign max max if don't want alter (max = (max < b) ? b : max).

an if-statement much more clear:

if (max < b) max = b;

and if need expression, can (ab)use short-circuit-evaluation of and:

(max < b) && (max = b)

btw, if want avoid repeating variable names (or expressions?), utilize maximum function:

max = math.max(max, b);

javascript ternary-operator

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -