Reputation: 123
What purpose does the question mark serve in the following code snippet:
Template.lists.selected = function () {
return Session.equals('list_id', this._id) ? 'selected' : '';
};
Upvotes: 1
Views: 283
Reputation: 12138
It's known as a ternary operator in a number of languages. It's a shortcut for a full-on if-then
statement.
Instead of writing this:
Template.lists.selected = function () {
if(Session.equals('list_id', this._id)) {
return 'selected';
}
else {
return '';
}
};
You do this:
Template.lists.selected = function () {
return Session.equals('list_id', this._id) ? 'selected' : '';
};
The if
return is immediately after the question mark; the else
return is after the colon.
Upvotes: 6