admin管理员组文章数量:1328578
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
2 Answers
Reset to default 8So 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.
本文标签: How is division before multiplication in JavascriptStack Overflow
版权声明:本文标题:How is division before multiplication in Javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742259386a2442228.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论