Anderson Green
Anderson Green

Reputation: 31850

Writing if-statements more concisely

In JavaScript (and most other programming languages), I've noticed that it's difficult to write if-statments concisely when checking multiple coniditions for the same variable, and performing the same action for each condition. Is it possible to write if-statements more concisely in scenarios like this?

if(x==1|x==2|x==3){ //Is there any way to make this less verbose?
    console.log("X equals either 1 or 2 or 3!");
}

//this isn't syntactically correct, but it's more concise,
//and I wish it were possible to write it more like this
if(x==(1|2|3)){
    console.log("X equals either 1 or 2 or 3!");
}

Upvotes: 0

Views: 400

Answers (5)

Dan
Dan

Reputation: 1179

Yeah, I've often wondering about a short hand for multiple if statements using the same value.

If you got some free time, I would recommend exploring some of JS's functional programming constructs. You might be able to achieve a more elegant solution.

I couldn't come up with a good or statement off the top of my head, but 'AND' seem like a no brainier.

var Cond = {
    'and': function(val, predicates) {
        return predicates.every( function(predicate) { return predicate(val) } );
    }
}

var predicates = [
    function(val) { return val > 40; }
    function(val) { return val < 45; }
    function(val) { return val === 42; }
];

console.log( Cond.and( 42, predicates ) );

My example is super lame but you should be easy enough to play around with.

Upvotes: 1

stefreak
stefreak

Reputation: 1450

In my experience, most of the time you can make if statements much more concise just by defining a method which returns a bool. You make the code much more readable, it's easier to test and maybe you are even reusing more code this way.

Of course the other answers are handy too.

One example as requested:

if (password.length < 6 || ! /[0-9]/.test(password) || password == userName) {
    alert('password doesn\'t meet security standards!');
}

vs.

function isSecurePassword(pw, userName) {
    if (password.length < 6) return false;
    if (/[0-9]/.test(password)) return false;
    if (password == userName) return false;

    return true;
}

if ( ! isSecurePassword(pw, userName)) {
    alert(..);
}

(normally you likely would have objects and methods and wouldn't have to pass around variables)

Upvotes: 1

Ted Hopp
Ted Hopp

Reputation: 234857

You can use this:

if ([1, 2, 3].indexOf(x) >= 0) { ... }

If you need a more complex test of equality, you can define your own function and use it with the built-in some() iterator:

function match(value1, value2) { var result = . . .; return result; }

if ([1, 2, 3].some(match.bind(null, x))) { . . . }

(bind appeared in JS 1.8.5; if you need backward compatibility, you can use:

if ([1, 2, 3].some(function(elt) {return match(x, elt);})) { . . . }

Upvotes: 2

Ejaz
Ejaz

Reputation: 8872

or

if([1, 2, 3].indexOf(x) !== -1){} //since JS 1.6

Array.indexOf

Upvotes: 1

Brian Ustas
Brian Ustas

Reputation: 65559

You could use a regular expression:

if (/^(1|2|3)$/.test(x)) { ... }

Upvotes: 3

Related Questions