Search code examples
javascriptternary-operatornull-coalescing-operator

Javascript null coalescing help, how can I incorporate a threshold value? a = b || c but if b > d, choose c


I want to assign a value to a variable in javascript

var a = b || c; //however if b > 200 choose c

Is there a simple way to do this?

var a = (b && b <= 200) ? b : c;

Thanks for any tips or advice. Just trying to write this as cleanly as possible.


Solution

  • var a = b;
    if(b == undefined || b > 200){
        a = c;
    }