最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How is division before multiplication in Javascript? - Stack Overflow

programmeradmin7浏览0评论

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.

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.

Share Improve this question asked May 14, 2018 at 14:43 ZeroZero 715 bronze badges 11
  • 7 "even if I know that multiplication have higher precedence then division" that's a pretty big assumption. Multiplication and division have the same priority, and they associate left-to-right. See here – Federico klez Culloca Commented May 14, 2018 at 14:45
  • 1 About operator precedence in JS – Gabriel Carneiro Commented May 14, 2018 at 14:46
  • 1 @MátéSafranka - It's better written vertically. It's really (PE)(MD)(AS). – T.J. Crowder Commented May 14, 2018 at 14:48
  • 2 There's something deeply ironic about the mnemonic acronym for operator precedence needing parentheses. – Máté Safranka Commented May 14, 2018 at 14:50
  • 1 @TJWolschon - Yeah, I understand PEMDAS is more U.S., BODMAS more UK. My ment above should have been "For Brits:" not "For Americans:" (I'm both, so I get confused sometimes.) – T.J. Crowder Commented May 14, 2018 at 14:55
 |  Show 6 more ments

2 Answers 2

Reset to default 8

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

See this fiddle: https://www.w3schools./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.

发布评论

评论列表(0)

  1. 暂无评论