To know if grouping with parentheses is really needed or not it is just a matter of know what comes first and to know that you just need to review what the precedence values are for each operator that is to be used an expression. Using a group operator where it is not needed might not change the outcome, and it might help to make the code more readable for developers that do not understand order of operations as well as they maybe should. However it still might be best to just understand order of operations so that unneeded use of group operators does not end up happening.
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.
So subtraction is a good example of an operator where associativity matters because taking 2 from 5 is not the something as taking 5 from 2.
Here subtraction is an example of left to right associativity, you start with 5 and then subtract 2 in the first example, things flow from left to right.
Although many operators have left to right associativity, many have the inverse of this also. One example is the logical not operator. This operator converts and inverts the boolean value of the value that is given to it at the right of it. If a value is given to the left that will result in an error.
Here in the first expression the logical not operator is preformed first because it has a value of 16, and multiplication is 14. So then not 0 converts to the boolean value true, then the multiplication operation is preformed resulting in 5. Finally the true boolean value is added to 5, when doing so true converts to the number 1 resulting in a number value of 6.
By grouping the 0 and one together the addition operation is now preformed first because the grouping precedence value of 20 superseding the value of the logical not operator again at 16. So now when the logical not operator is preformed this results in not 1 which results in a false boolean value that will convert to 0 when converted to a number, resulting in zero being multiplied by 4 which is 0. So no matter what else is going on anything inside the parentheses or grouping if you prefer will be preformed first.
The new operator is used with a constructor function as a way to create a new instance of that constructor function. There are many built in constructor functions such as the Date and Array constructor, but it is also possible to create a user define constructor function also. The Precedence of the new keyword is just below that of a function call, but still fairly high so that if I am creating a new instance of an object in an expression more often then not that will be preformed first in many expressions in which I would do such a thing.
There are the increment and decrement operators that are two plus signs, or negative signs. This operator can be placed before of after a variable that is to be incremented or decremented. If one of them is used after a variable then it is postfix and has a precedence value of 17.
Here we have the logical not operator that is one such operator that i find myself using the most often in this group. So when working out an expression any values that have there truth values inverted will be preformed before addition or subtraction. However although addition both in terms of numbers and strings will be preformed afterworlds, this group contains both Unary Plus, and Negation as well as Prefix Increment and Decrement.
Two multiplication operators can be used as a short hand for the Math.pow method. When doing this it will have a higher precedence over that of plain old multiplication.
The arithmetic operations of multiplication, division and remainder have a Precedence 15 which is one level above that of addition and subtraction. This is then one of the most commonly used set of expressions so it is a good idea to get this one solid at least when it comes to various expressions that involve addition, and subtraction with multiplication, and division.
Addition and subtraction have a Precedence of 14 so these operations will be preformed after Multiplication, Division, and Remainder.
Bitwise Shift operators are a way to shift the binary values of of numbers to the right or left. When using these they have an even lower Precedence to that of addition, subtraction, division and so forth. So be sure to use the grouping operator as needed when working out expressions with them.
The less than, greater than, less than or equal to, and greater that or equal to operators have a Precedence of 12. This level or Precedence also includes the in an instance of operators also.
The equality, inequality, as well as the strict forms of these operators have a Precedence value of 11.
So logical or operators have left to right associativity. In addition of anything that comes along evaluates to true that will be the value of the expression any any additional parts will not effect the result. This effect is desirable in many situations as such it is often used as a way to feature test, and create poly fills.
I often seen Conditional operators used in expressions. When using them any expression that comes first will typically be preformed first because just about all other operators typically used to write expressions have higher precedence.
Say you want to estimate the amount of money that you might make for a blog post if you manage to rank at the top of a search engine result page. You know the score that a keyword of interest gets relative to a compare keyword to which you know the average money traffic. You also know what is average when it comes to click threw rates for the first position, second position and so forth, and also your average page revenue per mille.
So in order to figure estimates for amount of money you might make for each rang position you will need to work out some kind of lengthly expression and use that in a function in which you pass arguments for all of this.
So you might end up with something like this:
So getting back to the subject of this post the expression that is used in the pageMoney function is composed of operators that are all division and multiplication, both of which have the same operator precedence, as well as associativity. So for this expression the operations are just simply preformed from left to right.
Say you want to write a function that will spit out a value between zero and one from zero up to one and then back down again depending on a current frame index value compared to a total max frame count. These are the kinds of functions I end up writing when I am playing around with animations that are governed by logic that is writing in a functional, deterministic kind of way.
In this exercise I made a function that gives a value that behaves as expected and when doing so wrote several expressions that make use of a few operators including a native function call.
The particular expression of interest here is the one that returns the value between zero and one depending on the current state provided via the functions arguments. This expression was fairly easy for me to write because I have a decent grasp on order of operations these days, however in the past it would have taken a lot longer as I would have followed a kind of time consuming trial and error process.
So where I live I do not have and kind of hard wired broadband Internet access, just mobile broadband via my cell phone. So with my plan I only have so much high speed data until I get throttled down to 128kbps, as such I need to budget my data or pay out the node for a higher data cap.
With that in mind it would be nice to know a certain figure each day that will tell be if I am above or below budget when it comes to data. If I am above budget I can watch a video or two, if not I have to change my browsing habits and focus more on work which does not eat up a whole lot of data as I just need to push and pull text. So to write some kind of function that can help me get that data target figure I can exercise my knowledge of operator precedence to work out an expression that will do just that.
Here is yet another real world example that is a function that helps figure the monthly payment of a fixed rate mortgage.