Reputation: 1717
I am reviewing some code around a JavaScript confirm call. From my understanding, confirm returns true or false. The developer I am working with keeps doing the following:
function askTheUser(question, myCallback) {
var result = confirm(question);
myCallback(result ? 2 : 1);
}
The line that is throwing me for a loop is the result ? 2 : 1
. Why would someone do that instead of just myCallback(result);
Is there something related to callbacks that I'm not aware of. I'm just interested in returning the true or false associated with whether a user confirmed the question or not. I keep looking at it. It just looks incorrect.
Thank you.
Upvotes: 0
Views: 259
Reputation:
result ? 2 : 1
means that if result
is true then 2 is returned otherwise 1
He/She must have a special reason to do so. Try looking the code where it's being used.
Upvotes: 0
Reputation: 1074158
It's not incorrect, it's just converting true/false
into 2/1
. Presumably they have some reason for doing that.
Upvotes: 1
Reputation: 7082
Yes you're right. It should just be (result). The callback can then assign 1, 2 or whatever it wants - if its really necessary to do so! It is simply going to invoke the callback with the parameter, there's nothing special about it
Upvotes: 0