I am writing my own function which returns lower argument between two arguments.
My first solution was:
function min(a, b) {
if (a < b)
return a;
else
return b;
}
console.log(min(0, 10));
// → 0
But I wanted to simplify it and wrote another one function:
function min(a, b) {
return a ? a < b : b;
}
console.log(min(0, 10));
// → true
Why my second function returns boolean value instead of number? Can I change this behavior?
a ? a < b : bwhat does this expression mean?Can I change this behavior?- what you need to change is your understanding of howcondition ? whentruthy : whenfalseyworks :pa? a < b : bshould return b (i.e. 10), not true.