乐趣区

JavaScript运算符算术运算符六

目录:

  1. 什么是算术运算符?
  2. 算术运算符有哪些?

一、什么是算术运算符?

运算符是处理数据的基本方法,用来从现有的值得到新的值。JavaScript 提供了多种运算符,覆盖了所有主要的运算。

二、算术运算符有哪些?

  1. 加法运算符:x + y
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>

<script type="text/javascript">
var num1 = 1;
var num2 = 2;

console.log(num1+num2);//3
</script>

</body>
</html>

2. 减法运算符:x – y

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>

<script type="text/javascript">
var num1 = 2;
var num2 = 1;

console.log(num1-num2);//1
</script>

</body>
</html>

3. 乘法运算符:x * y

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>

<script type="text/javascript">
var num1 = 2;
var num2 = 1;

console.log(num1*num2);//2
</script>

</body>
</html>

4. 除法运算符:x / y

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>

<script type="text/javascript">
var num1 = 9;
var num2 = 3;

console.log(num1/num2);//3
</script>

</body>
</html>

5. 余数运算符:x % y

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>

<script type="text/javascript">
var num1 = 9;
var num2 = 3;

console.log(num1%num2);//0
</script>

</body>
</html>

下节预告:赋值运算符

退出移动版