# Javascript operator precedence 5-4+2\*3

**URL:** <https://forum.freecodecamp.org/t/javascript-operator-precedence-5-4-2-3/330722>\
**Category:** General\
**Created:** [December 4, 2019, 7:03pm UTC](https://forum.freecodecamp.org/t/javascript-operator-precedence-5-4-2-3/330722 "2019-12-04T19:03:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![JohnRipper](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/johnripper/32/127995_2.png) [@JohnRipper](https://forum.freecodecamp.org/u/JohnRipper)\
**Post date:** [December 4, 2019, 7:03pm UTC](https://forum.freecodecamp.org/t/javascript-operator-precedence-5-4-2-3/330722/1 "2019-12-04T19:03:40Z")

</div>

From the documentation, [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator\_Precedence](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence), it appears that the order of operator precedence is \*, +, -, etc (highest to lowest).

Though when I tried evaluating 5-4+2\*3 , I got 7 as an answer in the console.

This meant the operation was evaluated in this way (5-4)+(2\*3) to give 7.

Yet I expected the operation to be evaluated in this way 5-(4+(2\*3)) to give -5

My main question is how come it appears that subtraction was evaluated first BEFORE addition to give 7. Yet it should be the opposite.

Am aware that I can use brackets to dictate the order of evaluation. I just wonder why this is happening the way it is.

---

<div class="post-metadata">

**Author:** ![jucarlospm](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jucarlospm/32/128851_2.png) [@jucarlospm](https://forum.freecodecamp.org/u/jucarlospm)\
**Post date:** [December 4, 2019, 7:26pm UTC](https://forum.freecodecamp.org/t/javascript-operator-precedence-5-4-2-3/330722/2 "2019-12-04T19:26:25Z")

</div>

It is because of the level of precedence. + and - have the same level of precedence and then proceed to read from left to right.

Now here the following actually happened

5-4 + 2 \* 3 (Multiply 2 \* 3)  
5-4 + 6 (Subtract 5-4)  
1 + 6 (1 + 6 is added)

More information: [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator\_Precedence](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence)

---

<div class="post-metadata">

**Author:** ![JohnRipper](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/johnripper/32/127995_2.png) [@JohnRipper](https://forum.freecodecamp.org/u/JohnRipper)\
**Post date:** [December 6, 2019, 4:44am UTC](https://forum.freecodecamp.org/t/javascript-operator-precedence-5-4-2-3/330722/3 "2019-12-06T04:44:12Z")

</div>

Oooh I see now. I actually hadn’t realized that:

1. -,+ have the same level of precedence and are evaluated from left to right.
2. \*, / , % also have the same level of precedence and are evaluated from left to right.

Thank you so much for the clarification.

---

<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, 11:31am UTC](https://forum.freecodecamp.org/t/javascript-operator-precedence-5-4-2-3/330722/4 "2021-01-16T11:31:17Z")

</div>

Closed on January 16, 2021, 11:31am UTC


