在 JavaScript 中的算术运算符如下

  • +
  • -
  • *
  • /
  • %

算术运算符的优先级和结合性

* / % 的优先级要高于 + -,无论是 + - * / %都是左结合性(从左至右计算)

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Demo</title>
<script>
let resultOne = 1 + 1;
console.log(resultOne);

let num1, num2;
num1 = 3;
num2 = 5;

let resultTwo = num1 + num2;
console.log(resultTwo);

let resultThree = num1 + 3;
console.log(resultThree);
</script>
</head>
<body>
</body>
</html>

JavaScript-算术运算符_html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Demo</title>
<script>
let resultOne = 2 - 1;

console.log(resultOne);

let numOne, numTwo;
numOne = 3;
numTwo = 9;

let resultTwo = numOne - numTwo;
console.log(resultTwo);
</script>
</head>
<body>
</body>
</html>

JavaScript-算术运算符_结合性_02

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Demo</title>
<script>
let result = 1 + 2 * 5;

console.log(result);
</script>
</head>
<body>
</body>
</html>

JavaScript-算术运算符_算术运算符_03

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Demo</title>
<script>
let result = 1 / 3;

console.log(result);
</script>
</head>
<body>
</body>
</html>

JavaScript-算术运算符_html_04

取余

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Demo</title>
<script>
/*
被除数 除数 商 余数
10 ÷ 3 = 3 1
*/

let result = 10 % 3;
console.log(result);
</script>
</head>
<body>
</body>
</html>

JavaScript-算术运算符_算术运算符_05