0

I am completely new to programming. I got a programming book for Christmas and since then I have been following the JavaScript book.

I dont understand how the ternary (or conditional) operator works. in the following example I know that if x is greater than 0 it runs the code but what does x:-x do?

                          x > 0 ? x : -x
Tibos
  • 26,262
  • 4
  • 43
  • 59
  • No, it runs code no matter which condition is met, it just runs different code. `if x > 0` then run `x`. Otherwise run `-x` – Royal Bg Jan 13 '14 at 09:25
  • This is not a "truthy and falsy operator" but a "[conditional operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator)". – h2ooooooo Jan 13 '14 at 09:27
  • possible duplicate of [What does this symbol mean in JavaScript?](http://stackoverflow.com/questions/9549780/what-does-this-symbol-mean-in-javascript) – Quentin Jan 13 '14 at 09:27
  • possible duplicate of [JavaScript ternary operator example with functions](http://stackoverflow.com/questions/10323829/javascript-ternary-operator-example-with-functions) – Maximin Jan 13 '14 at 09:43

7 Answers7

1

It is a shorter way of writing the following code:

   if(x > 0)
   {
      //then x
   }
   else
   {
      //then -x
   }

This syntax is very useful for e.g.

var myvar = (x > 0) ? x : -x;
Yami
  • 1,230
  • 1
  • 8
  • 14
1

You are looking at the conditional operator. It is a ternary operator (operator that takes 3 operands), somewhat similar in function to the if statement.

var y = (x > 0) ? x : -x // parenthesis for extra clarity

is almost equivalent to this:

  var y;
  if (x>0) { 
    y = x;
  } else {
    y = -x;
  }

A plain text translation is that if the value before the ? evaluates to true, the expression is evaluates to the value before the :, otherwise it is evaluated to value after the :.

The conditional operator doesn't need to be used in assignments only. This piece of code does one or another thing depending if x is larger than 0 or not.

x > 0 ? doOneThing(x) : doAnotherThing(x);

However, due to the overlapping functionality with the if statement (explained above), in cases like this, it is often avoided because it is not as clear. In assignments, like the one mentioned in the first example it is clearer and more concise. The if would have some code repetition (y =) and both branches need to be examined before identifying that that if just assigns a value to y. With the conditional operator that fact is immediately apparent.

Tibos
  • 26,262
  • 4
  • 43
  • 59
0

That's a ternary operator

if the condition is verified then you run the first part of the statement after (before the :) else you run the other part.

in the case:

y = x > 0 ? x: -x

you should read this y equal x if x > 0 else y equal -x

astreal
  • 3,072
  • 18
  • 33
0

the ? is called the ternary operator and is the only operator to have three operands (three parts to it). The statement reads like an if else statement. The bit before the question mark is the if statement.

var y = x > 0 ? x : -x;

Could also look like;

if( x > 0 ) { 
  var y = x;
} else {
  var y = -x;
}

Here it is broken down

x > 0 ?

before the question mark is the condition to be met (or if statement)

? x :

The bit after the question mark but before the colon is the code to run / assign if the condition evaluates to truthy.

: -x

The bif after the colon is the code to run / assign if the condition evaluates to falsy

Mark Walters
  • 10,729
  • 6
  • 31
  • 47
0

Truthy: Something which evaluates to TRUE. Falsey: Something which evaluates to FALSE.

There are only five falsey values in JavaScript:

undefined, null, NaN, 0, "" (empty string), and false

Anything other than above is truthy.

The operator you are using is ternary operator which behave as:

   condition ? execute this expression if true : execute this expression if false

DEMO

var x = 5;
x > 0 ? x : -x  # 5

var x = -1;
x > 0 ? x : -x  # 1 (positive of -1 as a result of minus operator in -x)
brg
  • 7,332
  • 8
  • 44
  • 60
0

I can explain this as I get myself:

 x > 0 ? x : -x

If x>0, then value should be x, otherwise, the value should be -x.

So basically this is a short form of using if and else. Like:

string name=firstname!=null?firstname+" ":string.Empty
JulyOrdinary
  • 3,235
  • 6
  • 43
  • 73
0

To answer your question the -x part will return the negative value of x if x is lower than 0. If x is grater than 0 it will return the value ( x ) else it will return the negative value( -x ).

Ternary Operator

x > 0 ? x: -x

condition ? instruction to execute if condition is true : instruction to execute if condition is false