# Javascript operator precedence

**URL:** <https://forum.freecodecamp.org/t/javascript-operator-precedence/173714>\
**Category:** General\
**Created:** [February 9, 2018, 4:10pm UTC](https://forum.freecodecamp.org/t/javascript-operator-precedence/173714 "2018-02-09T16:10:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![zhouxiang19910319](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/zhouxiang19910319/32/98849_2.png) [@zhouxiang19910319](https://forum.freecodecamp.org/u/zhouxiang19910319)\
**Post date:** [February 9, 2018, 4:10pm UTC](https://forum.freecodecamp.org/t/javascript-operator-precedence/173714/1 "2018-02-09T16:10:41Z")

</div>

in YDKJS types and grammar there is this code snippet:

```auto
var a = 42;
var b = "foo";
var c = false;

var d = a && b || c ? c || b ? a : c && b : a;

d; // 42

```

And the author stated that the correct operation sequence is something like this:

```auto
((a && b) || c) ? ((c || b) ? a : (c && b)) : a

```

I can understand that in JS the operation precedent goes something like this :

&&\>|| but what about ? and : ?  
do they come together?

For the snippet above I understand the `((a&&b)||c)` part. but why `((c || b) ? a : (c && b))`

also could someone give me some more example of how to use conditional/ternary operator?  
i dont really get how it works…

---

<div class="post-metadata">

**Author:** ![DanCouper](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dancouper/32/158608_2.png) [@DanCouper](https://forum.freecodecamp.org/u/DanCouper)\
**Post date:** [February 9, 2018, 4:24pm UTC](https://forum.freecodecamp.org/t/javascript-operator-precedence/173714/2 "2018-02-09T16:24:47Z")

</div>

> [@zhouxiang19910319](#):
>
> also could someone give me some more example of how to use conditional/ternary operator?
> 
> i dont really get how it works…

Given `a` is a boolean value,

```auto
a ? b : c

```

is roughly equivalent to

```auto
if (a) {
  b
} else {
  c
}

```

I say roughly because a ternary is an expression (it represents a value, and can be written anywhere you want a value), not a statement (which is effectively an action - an if/else block is an example).

So

```auto
let a = 100; // assume this value is coming from somewhere else, so could be any number
let b = (a > 10) ? "var a is greater than ten" : "var a is not greater than ten"; 

```

(`b` will have a value of `"var a is greater than ten"`)

Which is not quite the same as

```auto
let a = 100;
let b;

if (a > 10) {
  b = "var a is greater than ten";
} else {
  b = "var a is not greater than ten";
}

```

Ternary are imo very useful because they are expressions; there are many situations where you want/need a value (e.g. within JSX when using React) not a statement.

---

<div class="post-metadata">

**Author:** ![ArielLeslie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/arielleslie/32/264841_2.png) [@ArielLeslie](https://forum.freecodecamp.org/u/ArielLeslie)\
**Post date:** [February 9, 2018, 4:25pm UTC](https://forum.freecodecamp.org/t/javascript-operator-precedence/173714/3 "2018-02-09T16:25:32Z")

</div>

Thing 1: In real life please don’t get this cute with shorthand. Other developers who read your code (including Future You) will be annoyed.  
 ![image](https://global.discourse-cdn.com/freecodecamp/original/3X/e/7/e746e85357d846bed468047aad8918730aa1a002.jpg)

Thing 2: The difference with `:` is that [it’s right associative](https://stackoverflow.com/questions/36319740/ternary-operator-associativity)

Thing 3: Without worrying about what I said in Thing 2, translate all of this into longhand and the order will make more sense:

```auto
if (a && b || c) {
   if (c || b) {
        d = a;
    }
    else {
        d = c && b;
   }
else {
    d = a;
}

```

---

<div class="post-metadata">

**Author:** ![zhouxiang19910319](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/zhouxiang19910319/32/98849_2.png) [@zhouxiang19910319](https://forum.freecodecamp.org/u/zhouxiang19910319)\
**Post date:** [February 9, 2018, 4:34pm UTC](https://forum.freecodecamp.org/t/javascript-operator-precedence/173714/4 "2018-02-09T16:34:44Z")

</div>

i see. thanks for the warning LOL.  
i was simply following YDKJS instruction.

I think I will make my code more readable by not using all those “shortcuts”.

also the meme is on point 😄

---

<div class="post-metadata">

**Author:** ![zhouxiang19910319](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/zhouxiang19910319/32/98849_2.png) [@zhouxiang19910319](https://forum.freecodecamp.org/u/zhouxiang19910319)\
**Post date:** [February 12, 2018, 5:01am UTC](https://forum.freecodecamp.org/t/javascript-operator-precedence/173714/5 "2018-02-12T05:01:15Z")

</div>

wait you left out one `}` in your code I think. XD

---

<div class="post-metadata">

**Author:** ![ArielLeslie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/arielleslie/32/264841_2.png) [@ArielLeslie](https://forum.freecodecamp.org/u/ArielLeslie)\
**Post date:** [February 12, 2018, 5:12am UTC](https://forum.freecodecamp.org/t/javascript-operator-precedence/173714/6 "2018-02-12T05:12:47Z")

</div>

Quite possibly. I didn’t check it.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 16, 2021, 6:02pm UTC](https://forum.freecodecamp.org/t/javascript-operator-precedence/173714/8 "2021-01-16T18:02:50Z")

</div>

Closed on January 16, 2021, 6:02pm UTC


