JavaScript 中的算术运算符是什么?
javascriptweb developmentfront end technology更新于 2024/7/14 17:53:00
JavaScript 支持以下算术运算符。假设变量 A 为 10,变量 B 为 20,则 −
Sr.No | 运算符和说明 |
---|---|
1 | +(加法) 将两个操作数相加 例如: A + B 将得到 30 |
2 | - (减法) 从第一个操作数中减去第二个操作数 例如: A - B 将得到 -10 |
3 | * (乘法) 将两个操作数相乘 例如: A * B 将得到 200 |
4 | / (除法) 用分子除以分母 例如: B / A 将得到 2 |
5 | % (模数) 输出整数除法的余数 例如: B % A 将得到 0 |
6 | ++ (增量) 将整数值增加一 例如: A++ 将得到 11 |
7 | -- (减少) 将整数值减少一 例如: A-- 将得到 9 |
以下代码展示了如何在 JavaScript 中使用算术运算符。
示例
<html> <body> <script> <!-- var a = 33; var b = 10; var c = "Test"; var linebreak = "<br />"; document.write("a + b = "); result = a + b; document.write(result); document.write(linebreak); document.write("a - b = "); result = a - b; document.write(result); document.write(linebreak); document.write("a / b = "); result = a / b; document.write(result); document.write(linebreak); document.write("a % b = "); result = a % b; document.write(result); document.write(linebreak); document.write("a + b + c = "); result = a + b + c; document.write(result); document.write(linebreak); a = ++a; document.write("++a = "); result = ++a; document.write(result); document.write(linebreak); b = --b; document.write("--b = "); result = --b; document.write(result); document.write(linebreak); //--> </script> Set the variables to different values and then try... </body> </html>