Ternary operator with return statements JavaScript [duplicate] Ternary operator with return statements JavaScript [duplicate] jquery jquery

Ternary operator with return statements JavaScript [duplicate]


You can return from a ternary operator in javascript like this:

return sort.attr('selected') ? true : false;


You cannot assign a return statement to a variable. If you want active to be assigned the value true or false, just delete the returns:

var active = sort.attr('selected') ? true : false;

or maybe better:

var active = sort.prop('selected');

since .prop always returns true or false, regardless of the initial tag attribute.


why not just this?

 return sort.attr('selected');