JavaScript #7 ~ Operator Precedence

hjbaek91·2021년 5월 22일

[Learn] JavaScript

목록 보기

This will be another quick post on operator precedence within JavaScript code. This is a rather interesting concept to talk about as there seems to be an order of how code in JavaScript works in general. Depending on the operator we are using in our code, the order may start from either left-to-right or right-to-left.

Lets say we have written a code just like the example below. Which operator would have the highest or the lowest priority? According to the rules on MDN, we would need to calculate the values inside the paranthesis from left-to-right). Then, we would naturally divide the 3 to the get the final result. But we are not done yet. After getting the result, we then think about the operator precedence on the equal sign. Checking the rules again, the assignment operators have the lowest priority on the code and is read from right-to-left. As such, the result is inserted into the 'y' variable first and then the 'x' variable. Thus, we would be done!

let x = y = (1 + 2) / 3

The thing to take away from this concept is that there are rules that follows a certain order to how code works within variables and values. As such, knowing such rules will help when working with more complicated codes.

More information can be found on the url below listing all operator precedence rules:


Improving Everyday

0개의 댓글