简体   繁体   中英

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?

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM