Tsuna
Tsuna

Reputation: 2196

is there a way to check if all the variables are empty?

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

Answers (2)

coffeeak
coffeeak

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

spongessuck
spongessuck

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

Related Questions