0

Well, sorry for the really dumb question... But I´m struggling to understand the If shorthand.

I´m trying to make a clamp function, but most of the examples are written on shorthand form. Example that i found here on Stack Overflow.

function clamp(num, min, max) {
  return num <= min ? min : num >= max ? max : num;
}

Okay, i now what is happening, but i don´t how is happening... Off course, I could just happilly copy and paste that... Nevertheless, that´s no use if i actually want to learn. Can somebody explain/deconstruct for me what is happening on the return statement?

Thanks and again, sorry for the dumb question.

  • It's simply `if (num <= min) { return min } else if (num >= max) { return max } else { return num; }` – VLAZ May 21 '20 at 13:08
  • Just lay it out like an if/then/else statement with indentation where `?` substitutes for `then` and `:` substitutes for `else`. For clarity, put brackets round the if-condition(s). – Gem Taylor May 21 '20 at 13:16

0 Answers0