Ternary operator with return statements JavaScript

JDillon522 picture JDillon522 · Oct 18, 2013 · Viewed 71.2k times · Source

I need to return true or false if an option in a drop down selected.

This is my code:

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

I get an error that the first return is unexpected.

Why?

Answer

George K picture George K · Apr 15, 2014

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

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