David542
David542

Reputation: 110083

How to combine two value checks into one line in javascript

I have the following code in javascript:

var seasonDropdown = TVContainer.find('select.season').val()
if (seasonDropdown == "-1" || !seasonDropdown) {seasonDropdown = null}

Is there a way to combine these two into one line?

Upvotes: 1

Views: 56

Answers (1)

Derek Pollard
Derek Pollard

Reputation: 7165

You could do the following:

var seasonDropdown = (TVContainer.find('select.season').val() == "-1" || !TVContainer.find('select.season').val()) ? null : TVContainer.find('select.season').val();

But honestly, you should prefer readability over a solution like this.


if you want a bit cleaner look instead, you could use this:

var seasonDropdown = TVContainer.find('select.season').val();
if (seasonDropdown == "-1" || !seasonDropdown) seasonDropdown = null;

Upvotes: 2

Related Questions