21

I found this snippet of code in my travels in researching JSON:

var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;

I'm seeing more and more of the ? and : notation. I don't even know what it is called to look it up! Can anyone point me to a good resource for this? (btw, I know what != means).

user2864740
  • 60,010
  • 15
  • 145
  • 220
webdad3
  • 8,893
  • 30
  • 121
  • 223
  • 1
    possible duplicate of [javascript if alternative](http://stackoverflow.com/questions/1688337/javascript-if-alternative) – Greg Hewgill Jul 23 '10 at 22:03
  • possible duplicate of [Question mark in JavaScript](http://stackoverflow.com/questions/1771786/question-mark-in-javascript) – Bergi Jul 30 '13 at 16:18

4 Answers4

31

It's called a Conditional (ternary) Operator. It's essentially a condensed if-else.

So this:

var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;

...is the same as this:

var array;
if (typeof objArray != 'object') {
    array = JSON.parse(objArray);
} else {
    array = objArray;
}
goetz
  • 2,018
  • 2
  • 30
  • 33
Matt Huggins
  • 81,398
  • 36
  • 149
  • 218
  • 22
    Actually it is CALLED a conditional operator, but it IS A ternary operator. A ternary operator is any operation that takes 3 inputs. In many contexts, however, ternary operator has become synonymous with conditional since the conditional is either the most famous or sometimes the only ternary operator present in that language. For example, ++ -- are called unary operators, and + - / are called binary operators, etc. But that's just semantics, good answer, plus 1. :) – Razor Storm Jul 23 '10 at 22:05
  • 1
    @Razor – Nice addition; also have a look at the ECMAScript 5 specification of the [Conditional Operator ( ? : )](http://ecma262-5.com/ELS5_Section_11.htm#Section_11.12). – Marcel Korpel Jul 23 '10 at 23:06
  • 1
    @Razor - +1 for your comment, and [according to the MDC](https://developer.mozilla.org/en/JavaScript/Reference/Operators/Special_Operators/Conditional_Operator) the conditional operator is the only ternary operator in Javascript. – Peter Ajtai Sep 20 '10 at 00:42
13

It's the ternary conditional operator -- basically,

if (condition) {
   a = 4;
}
else {
   a = 5;
}

becomes

a = condition ? 4 : 5;
Alexander Gessler
  • 45,603
  • 7
  • 82
  • 122
  • Like Razor pointer out in the accepted answer: It's actually the [JS conditional operator](https://developer.mozilla.org/en/JavaScript/Reference/Operators/Special_Operators/Conditional_Operator), which is the only ternary operator in JS. – Peter Ajtai Sep 20 '10 at 00:39
11

That’s called the conditional operator:

condition ? expr1 : expr2

If condition is true, the operator returns the value of expr1; otherwise, it returns the value of expr2.

Gumbo
  • 643,351
  • 109
  • 780
  • 844
2

Just read it like this:

result = (condition) ? (true value) : (false value);

place what ever you like in the 3 operators.

As many has compared it to an IF.. THEN structure, so it is.

BerggreenDK
  • 4,915
  • 9
  • 39
  • 61