DEV Community

Zouhair Sahtout
Zouhair Sahtout

Posted on

Operator Precedence

Hello everyone, I hope you're doing well.

Starting from now, I'm going to dive into some JavaScript do some review after finally completing my associate degree as a Web Full Stack Developer.
I thought it might be a good idea to fill in the gaps that I may have missed during these past two years, where I covered almost everything from HTML, CSS, and JavaScript to Docker and Microservices.

And because I'm now a huge fan of JavaScript, my plan is to dive deep into its intricacies, so let's jump in:
Operator precedence for those who aren't familiar, essentially determines how operators are parsed in relation to each other.
An example of this is when you have an arithmetic operation like 3 + 4 * 5.
The higher the precedence, the earlier the operation is performed with respect to operators with lower precedence.

In the example above, the operation begins with multiplication because it has a precedence of 12 (from left to right), followed by addition with a precedence of 11 (from left to right), which is lower. This is why in mathematics, multiplication and division typically take precedence over addition and subtraction. It's also worth noting that grouping or parentheses have a higher precedence, around 18, so they can change the order of operations.

Now, let's shift our focus to the code below:
Image description

Again, the "+" and "-" operators take precedence with a value of 11.
On the other hand, the ">" operator has a precedence of 9.

Usually, all the mathematical operators are executed before the comparison operators, and they are evaluated from left to right.
This is different from the assignment operators, which execute from right to left and have a lower precedence of 2, this behavior is normal.
Let's assume it executed from left to right; then, "x" would be assigned the value of "y," which is undefined.

Alright with that said, if you read till here, thank you so much, that really means a lot.

Top comments (0)