Operator precedence AKA order of operations in javaScript

When writing javaScript expressions knowing the order in which operations is important to make sure that desired results will always be achieved. So operator precedence or just simply the order of operations in javaScript is the order in which operations are preformed and in this post I will be covering some of the basics with this.

1 - Order of operations basics in javaScript

With operators some operators have higher precedence than others. For example multiplication has higher precedence than addition. So if an expression contains addition first and then multiplication from left to right, the multiplication operation will be preformed first and then the addition. If for some reason I want the addition to happen first in can use parentheses to achieve that.

console.log( 10 + 5 * 2 ); // 20
console.log( (10 + 5) * 2 ); // 30

So there is the questing of what operators are preformed first, and the direction in which they are preformed as well which is called Associativity.

2 - Associativity of operators

So Associativity is the direction in which operations are preformed such as left to right, or right to left. Operators like addition, subtraction and so forth have left to right associativity. However other operators such as the assignment, and logical not operator have right to left Associativity.

var a = 5 - 2;
var b = 2 - 5;
console.log(a,b); // 3 -3

Here subtraction is an example of left to right associativity.