What is "?:" notation in JavaScript?

21,875

Solution 1

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;
}

Solution 2

It's the ternary conditional operator -- basically,

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

becomes

a = condition ? 4 : 5;

Solution 3

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.

Solution 4

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.

Share:
21,875
webdad3
Author by

webdad3

Been programming for a while, and I can honestly say I've embraced my inner nerdiness. By doing so that has helped my programming skills as I work on a side project almost every night after coding for my job. I really think mobile application development is where the future of the industry is going. Most of my side projects are now related to the Windows Phone 8 and Windows 8. However, I do all my web work using PHP and MVC. I try to follow the mantra of keep it simple stupid! I'm focusing on networking and becoming part of the community of developers. Not only to help myself, but also to help others. Wisdom is earned through many failures (big and small). You can also follow me on Twitter!

Updated on May 22, 2020

Comments

  • webdad3
    webdad3 almost 4 years

    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).

  • Razor Storm
    Razor Storm almost 14 years
    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. :)
  • Marcel Korpel
    Marcel Korpel almost 14 years
    @Razor – Nice addition; also have a look at the ECMAScript 5 specification of the Conditional Operator ( ? : ).
  • Peter Ajtai
    Peter Ajtai over 13 years
    Like Razor pointer out in the accepted answer: It's actually the JS conditional operator, which is the only ternary operator in JS.
  • Peter Ajtai
    Peter Ajtai over 13 years
    +1 for naming the only ternary operator in Javascript correctly.
  • Peter Ajtai
    Peter Ajtai over 13 years
    @Razor - +1 for your comment, and according to the MDC the conditional operator is the only ternary operator in Javascript.