# Precedence and Associativity of Arithmetic Operators in TypeScript

Arithmetic Operators Precedence and Associativity in TypeScript

Precedence

If we talk about precedence of the Arithmetic operators, then you will see that primary operators are the highest precedence and then unary operators have precedence and the last are binary operators with the lowest precedence.

Associativity

If we talk about precedence of the multiplication and division operators then you will find, these operators contain the same precedence. Now let's see an example.

(a) 50/5*2 = 20.
(b) 50/5*2 = 5.

Then, what is corect result?

How can TypeScript determine which operator is applied first? The answer to this question is Associativity. An operator can either have left-right or right-left associativity. When any binary operators with the same precedence is applied with the same operand, then left-right associativity is applied. And if any unary and primary operator with the same precedence is applied with the same operand, then right-left associativity will be applied.

The given table tells you which operator has higher precedence and how to associativity on them. In this table precedence is given in decreasing order in other words precedence category one (1) has the highest priority.

 Precedence Category Symbol Name Associativity 1 Primary () ++(Postfix) --(Postfix) Paraenthesis Increment operatorDecrement operator Right-LeftRight-LeftRight-Left 2 Unary +---(Pretfix) ++(Pretfix) Unary  plusUnary  minusDecrement operatorIncrement operator Right-LeftRight-LeftRight-LeftRight-Left 3 Binary */% Multiplication DivisionModulus Left-RightLeft-RightLeft-Right 4 Binary +- AdditionSubtraction Left-RightLeft-Right

Do the following steps to create a program using Precedence and Associativity in Arithmetic Operators.

Step 1

Open Visual Studio 2012 and click on "File" menu -> "New" -> "Project". After that, a window is opened, enter the name of your application like "ArithmeticPrecedenceNAssociativity", then click on the Ok button.

Step 2

After Step 1 your project has been created. Solution Explorer, which is at the right side of Visual Studio, contains the js file, ts file, css file and html files.

Step 3

The code for the Arithmetic Operators Precedence and Associativity program:

ArithmeticPrecedenceNAssociativity.ts

 class ArithmeticPrecedenceNAssociativity {     Myfunction() {         var PreRes: number = 100 + 5 * 10;// Precedence         document.write("Precedence res=" + PreRes + "
");         var AssoRes: number = 100 / 5 * 2;//Associativity         document.write("Associativity res=" + AssoRes);     } } window.onload = () =>{     var call = new ArithmeticPrecedenceNAssociativity();     call.Myfunction();}

Note: In the above declared program, multiplication operation is performed first, because the multiplication operator has greater precedence than the addition operator. And in the associativity part, two operators (such as (/) and (*)) with the same precedence as the same operand, left-to-right associativity will cause the left-most operator to be applied first, in other words the divide operation is performed first and then the multiplication operation is performed.

default.html

           TypeScript HTML App

app.js

 var ArithmeticPrecedenceNAssociativity = (function () {     function ArithmeticPrecedenceNAssociativity() { }     ArithmeticPrecedenceNAssociativity.prototype.Myfunction = function () {         var PreRes = 100 + 5 * 10;         document.write("Precedence res=" + PreRes + "
");         var AssoRes = 100 / 5 * 2;         document.write("Associativity res=" + AssoRes);     };     return ArithmeticPrecedenceNAssociativity; })(); window.onload = function () {     var call = new ArithmeticPrecedenceNAssociativity();     call.Myfunction();};

Step 4

Output