Question mark and colon in JavaScript

前端 未结 7 1642
孤独总比滥情好
孤独总比滥情好 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:07

    ?: is a short-hand condition for else {} and if(){} problems. So your code is interchangeable to this:

    if(max != 0){
           hsb.s = 225 * delta / max
    }
    else {
           hsb.s = 0
    }
    

    MDN - Conditional (Ternary) Operator

提交回复
热议问题