Reputation: 2196
I have a bunch of variables and I want to check if all of them are empty strings other than keep on doing variable == "" || variable == ""
is there is better way?
for example I have:
var a = $('.a').val();
var b = $('.b').val();
var c = $('.c').val();
if((a == "") || (b == "") || (c == "")) {
//blah blah
}
Is there a way to shorten up the if statement?
Upvotes: 0
Views: 41
Reputation: 3130
I would do something as follows:
var a = $('.a').val().trim();
var b = $('.b').val().trim();
var c = $('.c').val().trim();
if(!a && !b && !c){
//blah blah
}
Upvotes: 2
Reputation: 1073
First of all, using or (||) will check if any are empty, not all of them.
Anyway, not really shorter if you only have 3 variables, but you could put them in an array and use every
:
var allEmpty = [a, b, c].every(function(val) {
return val == '';
});
if(allEmpty) ...
Upvotes: 1