How does this function work in JS?

Hello, I don't understand the function here:

function function1(a, b){

return a*(a<b)

+b*(b<=a);

What exactly does "a*(a<b)" do? That would initially return True/False, right? So if a is less than b, True, and then a* True?!?

(1 votes)
Loading...

Similar Posts

Subscribe
Notify of
2 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
jo135
1 year ago

That’s where True/False would come out first, right?

Yes, but JavaScript is now a weakly typified Language. The value false is thus simply converted ad hoc into the number 0 when a computing operation is required. The value true in 1.

And now you can figure out what the function is. Tip: you could also make the same with an if expression.