Ternary operator with return statements JavaScript

JavascriptJqueryTernary

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

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

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

Solution 2 - Javascript

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.

Solution 3 - Javascript

why not just this?

 return sort.attr('selected');

Solution 4 - Javascript

From the docs:

>Syntax
condition ? expr1 : expr2
Parameters
condition (or conditions) An expression that evaluates to true or false.
expr1, expr2
Expressions with values of any type.

You should pay attention to the Expressions with values of any type., the return xxx is not an expression.

From wikipedia:

>An expression is a syntactic construct, it must be well-formed: the allowed operators must have the correct number of inputs in the correct places, the characters that make up these inputs must be valid, have a clear order of operations, etc. Strings of symbols that violate the rules of syntax are not well-formed and are not valid mathematical expressions.

So, in your case you can use:

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

Solution 5 - Javascript

Just a comment on your code:

> sort.attr('selected')

seems to be using the jQuery attr method, which used to try and second guess what you wanted and return either the attribute or the property. I think in recent versions it returns the attribute always.

Anyway, the presence of the selected attribute only means that the item (an option element?) is the default selected option, it doesn't mean that it is the currently selected option. For that you need the selected property (jQuery prop method). And since the selected property is a boolean:

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

can simply be:

 sort.prop('selected');

or without jQuery:

 optionElement.selected;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJDillon522View Question on Stackoverflow
Solution 1 - JavascriptGeorge KView Answer on Stackoverflow
Solution 2 - Javascriptuser2625787View Answer on Stackoverflow
Solution 3 - JavascriptnightographView Answer on Stackoverflow
Solution 4 - JavascriptaircraftView Answer on Stackoverflow
Solution 5 - JavascriptRobGView Answer on Stackoverflow