# Javascript - 运算符

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

## 算术运算符

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

Sr.No.Operator & Description
1

+(加法)

2

-(减法)

3

*(乘法)

4

/(除)

5

％(取模)

6

++(增量)

7

-(递减)

```<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

==(等于)

2

!=(不等于)

3

> (大于)

4

< (小于)

5

>= (大于或等于)

6

<= (小于或等于)

```<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)

2

|| (OR)

3

!(NOT)

```<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)

2

| (OR)

3

^(XOR)

4

〜(NOT)

5

<< (左移)

6

>> (右移)

7

>>> (右移零)

```<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

=(赋值)

2

+=(添加和赋值)

3

-=(减法和赋值)

4

*= (乘法和赋值)

5

/=(相除和赋值)

6

％=(取模和赋值)

```<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

```<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 运算符是一元运算符，位于其单个操作数的前面，该操作数可以是任何类型。它的值是一个字符串，指示操作数的数据类型。

Number"number"
String"string"
Boolean"boolean"
Object"object"
Function"function"
Undefined"undefined"
Null"object"

```<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...```

## 猜你喜欢

Tony Bai · Go语言第一课 -〔Tony Bai〕

1 篇笔记
hsy 2022年11月24日 20:35

<body>

<script type="text/javascript">

let a=10

let b="hello"

let result

document.write(typeof b,"<br >")//string

document.write(typeof a,"<br >")//number

document.write(typeof result,"<br >") //undefined

result=(typeof b=="string" ? "B is String":"B is number")

document.write(result,"<br >") //B is String

result=(typeof a=="String"?"A is String":"B is number")

document.write(result) //B is number

</script>

</body>