11

I'm trying to check whether the variable y is less than x and greater than z, but this boolean expression is returning false for some reason. Does JavaScript allow boolean expressions to be written concisely like this? If so, what is the correct syntax?

x = 2;
y = 3;
z = 4;

if(x > y > z){
    alert("x > y > z"); //Nothing happens!
}
0

3 Answers 3

11

Try using the logical and operator:

if (x > y && y > z) {

to guarantee both conditions are true.

DEMO: http://jsfiddle.net/3sxvy/

If you need to put this into a function, you could try:

function compareNumbers(direction) {
    var inOrder = (function () {
        if (direction === "desc") {
            return function (current, before) {
                return current <= before;
            };
        } else if (direction === "asc") {
            return function (current, before) {
                return current >= before;
            };
        }
    })();
    var valid = true;
    for (var i = 2; i < arguments.length; i++) {
        if (!inOrder(arguments[i], arguments[i-1])) {
            valid = false;
            break;
        }
    }
    return valid;
}

if (compareNumbers("desc", 33, 5)) {
    console.log("Good");
} else {
    console.log("Bad");
}

DEMO: http://jsfiddle.net/kn6M4/1/

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

3 Comments

It would be better if I could create a function that would actually be able to parse "concise" boolean expressions like the one above.
The problem is that your "concise" expression has a meaning in JavaScript, but a different one (see my answer).
@AndersonGreen I don't know if it helps, but I added a function that could do the same as what the comparison is (in either ascending or descending order), with as many numbers as you pass to the function. It will really only work for numbers in the same ascending/descending order.
7

Change your test to

if (x > y && y > z){

When you write (x > y > z), this is equivalent to ((x>y)>z), so you're comparing a boolean (x>y) to z. In this test, true is converted to 1, which isn't greater than 2.

1 Comment

this answer is the best, as its concise, it gives the solution and explains why the syntax used by the OP is valid but has a different meaning in JS, bravo!
7

You want

if(x > y && y > z){
    alert("x > y > z"); //Nothing happens!
}

Javascript will try to parse your original statement from left to right and you'll end up comparing z to a boolean value which will then be parsed to a number (0 or 1).

So your original statement is equivalent to

if( (x > y && 1 > z) || (x <= y && 0 > z))

4 Comments

That's not as concise as the original, unfortunately. I was hoping that JavaScript would actually allow this kind of concise boolean expression syntax.
Yes, but it has the slight benefit of working! The original is evaluated left to right I believe, meaning once the first part was evaluated, the next evaluation would be if(true > z) { which makes no sense. The same mechanism that would allow your example to be more concise would not allow things like var x = y || z || g; which is a greater benefit than syntactical sugar.
@Ocelot20 thats correct, but JS will actually cast it to a number for the conversion, so its really equivalent to the statement in my updated answer. Which kind of makes sense but clearly isn't what the OP intended.
@ben336: Right I guess by "makes no sense" I meant, like you said, that it wasn't the original intent.

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.