Question mark and colon in JavaScript

前端 未结 7 1533
孤独总比滥情好
孤独总比滥情好 2020-11-21 11:36

I came across the following line

hsb.s = max != 0 ? 255 * delta / max : 0;

What do the ? and : mean in this conte

相关标签:
7条回答
  • 2020-11-21 12:16
    hsb.s = max != 0 ? 255 * delta / max : 0;
    

    ? is a ternary operator. It works like an if in conjunction with the :

    != means not equals

    So, the long form of this line would be

    if (max != 0) { //if max is not zero
      hsb.s = 255 * delta / max;
    } else {
      hsb.s = 0;
    }
    
    0 讨论(0)
提交回复
热议问题