无涯教程-Javascript - 运算符

让无涯教程用一个简单的表达式 4 + 5=9 。这里的4和5称为操作数," +"称为运算符,JavaScript支持以下类型的运算符。

  • 算术运算符
  • 比较运算符
  • 逻辑运算符
  • 赋值运算符
  • 三元)运算符

算术运算符

JavaScript支持以下算术运算符,假设变量A=10,变量B=20,则:

Sr.No. Operator & Description
1

+(加法)

两个数相加   ,如: A + B=30

2

-(减法)

第一个数减去第二个数 , 如: A-B=-10

3

*(乘法)

两个数相乘,如: A * B=200

4

/(除)

分子除以分母取整,如: B/A=2

5

%(取模)

相除后的余数,如: B%A=0

6

++(增量)

将整数值加1,如: A ++=11

7

-(递减)

将整数值减一,如: A--=9

注意  -  加法运算符(+)适用​​于数字和字符串。如" a" + 10将得到" a10"。

以下代码显示了如何在JavaScript中使用算术运算符。

<html>
   <body>
   
      <script type = "text/javascript">
         <!--
            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>

运行上面代码输出

a + b=43
a - b=23
a/b=3.3
a % b=3
a + b + c=43
++a=35
--b=8
Set the variables to different values and then try...

比较运算符

JavaScript支持以下比较运算符,假设变量A=10,变量B=20,则:

Sr.No. Operator & Description
1

==(等于)

检查两个值是否相等,如 :(A == B)=false。

2

!=(不等于)

检查两个值是否不相等,如:(A!= B)=true。

3

> (大于)

检查左数的值是否大于右数的值,如: (A > B)=false

4

< (小于)

检查左数的值是否小于右数的值,如: (A < B)=true.

5

>= (大于或等于)

检查左操作数的值是否大于或等于右操作数的值,如: (A >= B)=false.

6

<= (小于或等于)

检查左操作数的值是否小于或等于右操作数的值,如: (A <= B)=true

以下代码显示了如何在JavaScript中使用比较运算符。

<html>
   <body>  
      <script type = "text/javascript">
         <!--
            var a = 10;
            var b = 20;
            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) => ");
            result = (a >= b);
            document.write(result);
            document.write(linebreak);
         
            document.write("(a <= b) => ");
            result = (a <= b);
            document.write(result);
            document.write(linebreak);
         //-->
      </script>      
      Set the variables to different values and different operators and then try...
   </body>
</html>

运行上面代码输出

(a == b) => false 
(a < b) => true 
(a > b) => false 
(a != b) => true 
(a >= b) => false 
a <= b) => true
Set the variables to different values and different operators and then try...

逻辑运算符

JavaScript支持以下逻辑运算符,假设变量A=10,变量B=20,则:

Sr.No. Operator & Description
1

&& (AND)

如果两个数都不为零,则返回为true。如: (A && B)=true.

2

|| (OR)

如果两个数中的任何一个都不为零,则返回true。如:(A || B)=true。

3

!(NOT)

反转其操作数的逻辑状态,如果条件为true,则返回false。如: ! (A && B)=false.

尝试以下代码,以了解如何在JavaScript中实现逻辑运算符。

<html>
   <body>   
      <script type = "text/javascript">
         <!--
            var a = true;
            var b = false;
            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);
         //-->
      </script>      
      <p>Set the variables to different values and different operators and then try...</p>
   </body>
</html>

运行上面代码输出

(a && b) => false 
(a || b) => true 
!(a && b) => true
Set the variables to different values and different operators and then try...

按位运算符

JavaScript支持以下按位运算符,假设变量A=2,变量B=3,则:

Sr.No. Operator & Description
1

& ( AND)

对其整数参数的每一位执行布尔"AND"运算。如: (A & B) =  2.

2

| (OR)

对其整数参数的每一位执行布尔"OR"运算。如:(A | B) =  3。

3

^(XOR)

对其整数参数的每一位执行布尔异或(^)运算。如:(A ^ B)=1。

4

〜(NOT)

一元运算符,通过反转操作数中的所有位进行操作。如:(〜B)=-4。

5

<< (左移)

将值左移一个位置等同于将其乘以2,将两个位置左移等同于乘以4,新位填充零。如: (A << 1)  =  4.

6

>> (右移)

与左移相反,右移一位相等于除以2。如: (A >> 1)=1.

7

>>> (右移零)

该运算符类似于>>运算符,不同之处在于向左移的位始终为零。如: (A >>> 1)=1.

尝试使用以下代码在JavaScript中实现按位运算符。

<html>
   <body>   
      <script type = "text/javascript">
         <!--
            var a = 2; //Bit presentation 10
            var b = 3; //Bit presentation 11
            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("(~b) => ");
            result = (~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);
         //-->
      </script>      
      <p>Set the variables to different values and different operators and then try...</p>
   </body>
</html>

运行上面代码输出

(a & b) => 2 
(a | b) => 3 
(a ^ b) => 1 
(~b) => -4 
(a << b) => 16 
(a >> b) => 0
Set the variables to different values and different operators and then try...

赋值运算符

JavaScript支持以下赋值运算符:

Sr.No. Operator & Description
1

=(赋值)

将值从右侧操作数分配到左侧操作数 。如: C=A + B  等于将A + B的值赋给C

2

+=(添加和赋值)

将右操作数添加到左操作数,并将输出分配给左操作数。如 : C +=A 等于 C=C + A

3

-=(减法和赋值)

从左侧操作数中减去右侧操作数,并将输出分配给左侧操作数。如: C-= A 等于 C=C-A

4

*= (乘法和赋值)

将右操作数与左操作数相乘,并将输出分配给左操作数。如: C *=A 等于 C=C * A

5

/=(相除和赋值)

将左操作数除以右操作数,并将输出分配给左操作数。如: C/= A 等于 C=C/A

6

%=(取模和赋值)

使用两个操作数取模,并将输出分配给左操作数。如 : C%= A等于C=C%A

尝试以下代码在JavaScript中实现赋值运算符。

<html>
   <body>   
      <script type = "text/javascript">
         <!--
            var a = 33;
            var b = 10;
            var linebreak = "<br />";
         
            document.write("Value of a => (a=b) => ");
            result = (a = b);
            document.write(result);
            document.write(linebreak);
         
            document.write("Value of a => (a += b) => ");
            result = (a += b);
            document.write(result);
            document.write(linebreak);
         
            document.write("Value of a => (a -= b) => ");
            result = (a -= b);
            document.write(result);
            document.write(linebreak);
         
            document.write("Value of a => (a *= b) => ");
            result = (a *= b);
            document.write(result);
            document.write(linebreak);
         
            document.write("Value of a => (a /= b) => ");
            result = (a /= b);
            document.write(result);
            document.write(linebreak);
         
            document.write("Value of a => (a %= b) => ");
            result = (a %= b);
            document.write(result);
            document.write(linebreak);
         //-->
      </script>      
      <p>Set the variables to different values and different operators and then try...</p>
   </body>
</html>

运行上面代码输出

Value of a => (a=b) => 10
Value of a => (a += b) => 20 
Value of a => (a -= b) => 10 
Value of a => (a *= b) => 100 
Value of a => (a /= b) => 10
Value of a => (a %= b) => 0
Set the variables to different values and different operators and then try...

条件运算符(?:)

条件运算符首先对表达式的真值或假值求值,然后根据求值输出执行两个给定语句之一。

NO 描述
1

? :(三元运算符)

eg: x==2?X:Y; 如果x==2,则返回X,否则返回Y

请尝试以下代码,以了解条件运算符在JavaScript中的工作方式。

<html>
   <body>   
      <script type = "text/javascript">
         <!--
            var a = 10;
            var b = 20;
            var linebreak = "<br />";
         
            document.write ("((a > b) ? 100 : 200) => ");
            result = (a > b) ? 100 : 200;
            document.write(result);
            document.write(linebreak);
         
            document.write ("((a < b) ? 100 : 200) => ");
            result = (a < b) ? 100 : 200;
            document.write(result);
            document.write(linebreak);
         //-->
      </script>      
      <p>Set the variables to different values and different operators and then try...</p>
   </body>
</html>

运行上面代码输出

((a > b) ? 100 : 200) => 200 
((a < b) ? 100 : 200) => 100
Set the variables to different values and different operators and then try...

类型运算符

typeof 运算符是一元运算符,位于其单个操作数的前面,该操作数可以是任何类型。它的值是一个字符串,指示操作数的数据类型。

这是 typeof 运算符的返回值列表。

类型 由typeof返回的字符串
Number "number"
String "string"
Boolean "boolean"
Object "object"
Function "function"
Undefined "undefined"
Null "object"

以下代码显示了如何实现 typeof 运算符。

<html>
   <body>      
      <script type = "text/javascript">
         <!--
            var a = 10;
            var b = "String";
            var linebreak = "<br />";
         
            result = (typeof b == "string" ? "B is String" : "B is Numeric");
            document.write("Result => ");
            document.write(result);
            document.write(linebreak);
         
            result = (typeof a == "string" ? "A is String" : "A is Numeric");
            document.write("Result => ");
            document.write(result);
            document.write(linebreak);
         //-->
      </script>      
      <p>Set the variables to different values and different operators and then try...</p>
   </body>
</html>

运行上面代码输出

Result => B is String 
Result => A is Numeric
Set the variables to different values and different operators and then try...

参考链接

https://www.learnfk.com/javascript/javascript-operators.html