-1

Recently I came upon a weird line in a code(Second answer, couldn't find a way to link directly to the answer).
This is it:

reverse = !reverse ? 1 : -1;

Well, I guess specifically the use of boolean ? option : option;

This looks like legitimately nothing I have encountered so far. I cannot find it in google! Please explain what this is, what is the syntax and how to use it, because I really want to know what this is, since it looks like some advanced smart thing...

Is that a if-else statement? Thanks for reading this and answering! :D

Community
  • 1
  • 1
  • 2
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator – j08691 Mar 11 '15 at 15:10
  • 1
    Googling `?` is, admittedly, kind of difficult. But it's an operator, so try [Javascript operators](https://www.google.com/#q=Javascript+operators) and you would have probably found what you are looking for. Even then it's kind of buried. The first two results I got didn't even have it (but that's W3Schools for you). The third (also W3Schools, but their reference page - does have it). [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators) has it, but it's pretty buried in the "special" operators section. – Matt Burland Mar 11 '15 at 15:16
  • @MattBurland Thanks, my question was already answered and marked as a dublicate :( Thanks anyway! – The SuperCuber Mar 11 '15 at 15:18
  • @TheSuperCuber: I know it's answered already, I was trying to suggest some better ways to find the answer because it is genuinely difficult to google `?` and get anything meaning full as a result. It's even difficult to do on StackOverflow, it's an edge case as far as searching goes. Either way, you really need a better title for your question. – Matt Burland Mar 11 '15 at 15:21

2 Answers2

5

It's a ternary (conditional) expression:

var result = condition ? "true result" : "false result";

Example:

// since 1 is less than 2 result = "It's less!"
var result = 1 < 2 ? "It's less!" : "It's more!";
Aadit M Shah
  • 67,342
  • 26
  • 146
  • 271
tymeJV
  • 99,730
  • 13
  • 150
  • 152
1

The ? : is the ternary operator. It was invented to simplify stuff like this:

if (booleanExpression) {
    var x = expression1;
} else {
    var x = expression2;
}

So the above would become:

var x = booleanExpression ? expression1: expression2;

They are equivalent but using the ternary operator is a bit more concise. I've mostly seen this used for conditional assignment or just making an if-else clause more compact/require fewer keystrokes. In the example you gave, I would imagine it is used for conditionally reversing some collection based on an argument or flag provided to the module.

theWanderer4865
  • 811
  • 12
  • 20
  • It should be `booleanExpression ? expression1 : expression2`. The conditional operator can't have statements for its true and false branches. For example, you can't do `true ? var x = 1; var x = -1;`. That's invalid syntax. – Aadit M Shah Mar 11 '15 at 15:15
  • @AaditMShah Yes you can, but you should do `true ? var x=1*:* var x=-1;` – The SuperCuber Mar 11 '15 at 15:19
  • @TheSuperCuber __No. You can't.__ Press `Ctrl + Shift + I` and paste `true ? var x = 1; var x = -1;` into the console. It will give you a syntax error. – Aadit M Shah Mar 11 '15 at 15:22
  • @AaditMShah You still make the same mistake, look at my code, i highlighted your mistake with **. you should use `:` between the statements, but not `;` – The SuperCuber Mar 11 '15 at 15:27
  • @TheSuperCuber It's still a syntax error. Yes, I concede I made a typo when I used a semicolon instead of a colon. However, the argument was still valid. The conditional operator is an operator (i.e. it is an expression). An expression cannot have a statement within it. Hence `true ? var x = 1 : var x = -1;` is still a syntax error. – Aadit M Shah Mar 11 '15 at 15:35
  • @AaditMShah try the same without var, worked for me. I dont know what is going on. :D – The SuperCuber Mar 11 '15 at 15:42
  • @TheSuperCuber Of course it did. It's because `x = 1` is an expression. However, `var x = 1` is a statement. Why am I having such a stupid discussion? – Aadit M Shah Mar 11 '15 at 16:23
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/72778/discussion-between-the-supercuber-and-aadit-m-shah). – The SuperCuber Mar 11 '15 at 16:31
  • @TheSuperCuber Let's not. – Aadit M Shah Mar 11 '15 at 16:33
  • @AaditMShah Well then I'll just copy what I've said there: I have no idea, could you wait a second while I'm testing that with an .html document? Ill do the following: true ? `alert("Hi") : alert("Bye"); ` Also, gonna try that in the console now. Worked in the console. Watcha say? – The SuperCuber Mar 11 '15 at 16:35