9

This should be quite a simple question I just couldn't find the answer anywhere so thought I'd save myself some time and ask here.

I was looking at some javascript code and noticed an expression of the form:

a < b && (other maths statements);

The other maths statements are just assigning of variables and simple stuff.

My question is this. Is this another way to write compact if statements?

I know that statement?if-block:else-block; is one way to do it but as && is short-circuit and left-associative this is all I can think it would be. I know you can't have an else block with this method but it's pretty neat.

Can someone just validate I'm awake at this time in the morning please.

Thanks,

Taylan Aydinli
  • 4,333
  • 15
  • 39
  • 33
Blue42
  • 355
  • 2
  • 4
  • 13
  • This is just an boolean expression which will return either `true` or `false`. It can be used in an if statement. But without `if(a&&b)` or any asignment to a variable it does nothing... – Igl3 Nov 25 '13 at 10:14
  • 1
    Just dont do it. Readability/maintainability is more important than saying "Hey, I wrote the same thing in one line; isn't this cool?". If this is for some academic purpose, then its a different thing. – UltraInstinct Nov 25 '13 at 10:15
  • 2
    Yes, it works exactly as you think with its short-circuit evaluation. No, it's not necessarily better than writing `if (...)`, if only because it prompts questions like this. – deceze Nov 25 '13 at 10:15

2 Answers2

12

Yes, you can do it. If a is less than b the other statements are going to execute.

That said, you probably shouldn't do it. Rather than trying to save yourself a small amount of typing you should instead go with a more readable piece of code whose purpose is immediately obvious. After all, if you needed to check that you weren't misinterpreting it, other programmers who may have to read or modify your code could have the same problem.

There is also a pitfall with this if you were trying to do multiple assignments, but one of the middle assignments was for a "falsey" value. Consider this example:

a < b && (a = 1) && (b = 0) && (c = 3);

Essentially, if a is less than b, set a to 1, b to 0 and c to 3. However, the (b = 0) returns false (0 is considered false when converted to a boolean), so the (c = 3) part never executes. Obviously with that basic example anybody who is reasonably knowledgeable about JavaScript could work out that it will fail, but if the values for a, b and c were coming from elsewhere there'd be no immediate indication that it wouldn't always work.

Anthony Grist
  • 38,173
  • 8
  • 62
  • 76
  • Thought as much. I was just trying to understand some code I was given this morning. I would of used the conditional?if-block:else-block method if I really wanted to do it on one line, but this is an interesting way of looking at things. I like you're example too (but you're right, most people with a basic grasp of programming should realize that). – Blue42 Nov 25 '13 at 10:50
  • 1
    In that case though, would you not do `a < b && (a = 1, b = 0, c = 3);`? That is a rather good point though -- it might not be immediately obvious. – Qantas 94 Heavy Nov 30 '13 at 00:19
1

You can use this type of if-else statement for shorter code, although in my opinion it's less readable:

JavaScript:

var el = document.getElementById('test'),
a = 1,
b = 0;

a>b && (el.innerHTML = "True") || (el.innerHTML = "False");

HTML:

<div id="test"></div>

An example jsFiddle

If you switch the operator from greater-than to less-than, you will see "False" printed inside of the "test" DIV.

Trendy
  • 460
  • 2
  • 12
  • 2
    For a single assignment operation it makes more sense to use a ternary operator: `el.innerHTML = (a > b ? "True" : "False");`. Your example is closer to how you'd do that in a language like Lua, which lacks an actual ternary operator but has the same dynamic typing and implicit boolean conversion that JavaScript does. – Anthony Grist Nov 25 '13 at 10:26
  • 2
    I agree. The ternary operator is better example of a simplified if-else statement, especially in the simple case of a single assignment. Although, as we both stated in our answers, either example is less readable and makes your code harder to understand. – Trendy Nov 25 '13 at 10:41
  • 1
    You're right, readability is key! The ternary operator is fairly easy to understand though most people should of come across it at one point or another. The method I proposed is one that is novel to me so thanks for answering =) – Blue42 Nov 25 '13 at 10:52