Skip to content

Latest commit

 

History

History

07_operator_precedence

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

11. Operator Precedence

constnow=2037;constageEke=now-1997;constageSarah=now-2018;console.log(now-1997>now-2018);

Operator precedence is a collection of rules in JavaScript that reflect conventions about which procedure to perform first in a given mathematical expression.

Operators with higher precedence will be executed before the lower ones.

Let's take a look at the precedence of different operators from MDN.

Image Credit:MDN DOCS

Now we can use this table to understand the precedence of the operators in the code above and how it was executed.

On the table, subtraction - has a higher precedence than greater than > which is why the numbers were executed first before making the comparison.

On the table, we can also see which operators are executed from left to right.

For Example.

console.log(25-10-5);

Since subtraction is executed from left to right, the result of this calculation would be 10

letx,y;// Declare two variables in one linex=y=25-10-5;// x = y = 10. y = 10, x = 10conslole.log(x,y);

The result in the above example would be 10, 10. This is because JavaScript will look at the calculation and run the ones with the higher precedence (in this case -) first before the assignment operator (=).

constaverageAge=ageEke+ageSarah/2;console.log(ageEke,ageSarah,averageAge);

Here we are calculating the average value of ageEke & ageSarah and dividing it by 2

Result would be: 40 19 49.5

close