0

Perhaps I missed something, but is there a better/more concise way of writing something like this:

var a=1,
    b=2,
    c=3;
if(a===1 && b===1 && c===1){ do something };

or

if(a===1 || b===1 || c===1){ do something };

I'm trying to keep the code small, so am not looking for iterating through arrays, etc like proposed in pages I've come across. I would think that there would be something (at least for the first example where they are all the same) that would look like

if(a=b=c===1){ do something? };

Thanks!

3 Answers 3

4

You can take a functional approach, and create a simple comparison function generator:

function eq(val) {
    return function(x) {
        return x === val
    }
}

And then use .every for the &&:

if ([a,b,c].every(eq(1))) {
    // all were equal
}

Or use .some for the ||:

if ([a,b,c].some(eq(1))) {
    // at least one was equal
}

You could also create a function that receives the result of a condition and a function to invoke when the condition was true:

function when(cond, fn) {
    if (cond)
        fn.call.apply(fn, [].slice.call(arguments, 2))
}

And encapsulate your if body into a function...

function doIt(arg1, arg2) {
    console.log("doing it!")
    console.log(this, arg1, arg2)
}

Then use it like this:

when([a,b,c].every(eq(1)), doIt, null, "foo", "bar")

The third argument to when sets the this value of the callback, and subsequent arguments are passed as arguments to the callback.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 I like the functional approach, if it's something that comes up a lot.
2

No, there isn't. You have to check them individually. JavaScript just doesn't have an operator for what you're trying to do.

Your if(a=b=c===1){ ... (which I realize was just an example) would set a and b to be true (if c===1) or false (if it didn't) and then branch based only on c===1.

I mean, for that specific case, there are a couple of math approaches, but in the general case, no.

1 Comment

Although there are a lot of other excellent responses to this question, it seems that this one is the closest. @squint solution is nice and I may use that in something down the road though Thanks!
0

Depending on how much you want to invest, that would be a possible solution:

var slice=Function.call.bind([].slice)

var checkCondition=function(condition){ 
    return function(){
        return slice(arguments).reduce(function(o,n){
            return o && o == condition(n);
        },true);
    }
}

greater5=checkCondition(function(x){ return x>5; });

console.log(greater5(4,7));

If you only have one or two times in your code where you have to check multiple variables it would be absolutely overkill. But if you have multiple places with varying arguments, it might help.

You could easily define new checks by definition of another function and add as many variables as you want.

1 Comment

Keep in mind that .reduce() doesn't short circuit the evaluation, so the result you'll get will be whether the result of the last condition check matches the result of the second to last. So g5(4,4); // true...g5(4,4,4); // false...and a continued alternation. You could add short-circuiting to the evaluation in the .reduce() return value to handle it. return o && o == condition(n);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.