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 return
s:
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.