Zero
Zero

Reputation: 71

How is division before multiplication in Javascript?

I run some code and I get same results with or without parenthesis, even if I know that multiplication have higher precedence then division. Here is example:

let calculate = 16 / 30 * 100

I gott same result as

let calculate = (16 / 30) * 100

So I don't know which of them has higher precedence.

Upvotes: 5

Views: 1681

Answers (2)

B B
B B

Reputation: 64

See this fiddle: https://www.w3schools.com/code/tryit.asp?filename=FRB0CCQOKZT2

the code is evaluated in this order:

  • Parentheses
  • Multiply and Divide
  • Addition and Subtraction

If you have Multiple and Divide in the same equation then it evaluates the first one it encounters. you can override this by using parentheses.

Upvotes: 3

T.J. Crowder
T.J. Crowder

Reputation: 1074295

So I don't know which of them has higher precedence.

Neither, they have the same precedence and associativity; see MDN's page for details.

Almost all programming languages adhere to PEMDAS:

  • PE - Parentheses and Exponents
  • MD - Multiplication and Division
  • AS - Addition and Subtraction

...aka BODMAS:

  • BO - Brackets and Orders
  • DM - Division and Multiplication
  • AS - Addition and Subtraction

Upvotes: 8

Related Questions