下面是基本比较运算符的示例。
null
例1:
javascript
<script> function gfg() { let val1 = 5; // Equality Operators document.write(val1 == 5); document.write( "<br>" ); // Relational Operators document.write(val1 > 0); } gfg(); </script> |
输出:
truetrue
运算符能够操作特定的值或操作数。运算符用于对操作数执行特定的数学和逻辑计算。换句话说,我们可以说运算符操作操作数。在JavaScript中,运算符用于比较值、执行算术运算等。JavaScript支持多种运算符:
- 算术运算符
- 比较运算符
- 逻辑运算符
- 赋值运算符
- 三元算子
- 操作员类型
算术运算符: 有各种算术运算符——
- +(补充): “+”运算符对两个操作数执行加法。 例子:
Y = 5 + 5 gives Y = 10
- 注: “+”运算符也可用于连接(添加)字符串。 例子:
Y = "Geeks" + "for" + "Geeks" gives Y = "GeeksforGeeks"
- 例子:
Y = "Geeks" + 4 + "Geeks" gives Y = "Geeks4Geeks"
- –(减法): “-”运算符对两个操作数执行减法运算。 例子:
Y = 5 - 3 gives Y = 2
- *(乘法): “*”运算符对两个操作数执行乘法。 例子:
Y = 5 * 5 gives Y = 25
- /(分部): “/”运算符对两个操作数执行除法(将分子除以分母)。 例子:
Y = 5 / 5 gives Y = 1
- %(模数): “%”运算符给出 整数 分开 例子:
A % B means remainder (A/B) Y = 5 % 4 gives Y = 1
- ++(增量): “++”运算符将整数值增加1。 例子:
let A = 10 and Y = A + + then A = 11, Y=10 if A = 10 and Y = + + A then A = 11, Y=11
- –(减量): “–”运算符将整数值减少1。 例子:
let A = 10 and Y = A - - then A = 9, Y=10 if A = 10 and Y = - - A then A = 9, Y=9
分配操作员: JavaScript中有各种赋值运算符——
- =(赋值运算符): 将右操作数值赋给左操作数。 例子:
If A = 10 and Y = A then Y = 10
- +=(添加和分配运算符): 将左操作数和右操作数的值相加,然后将结果赋给左操作数。 例子:
Y += 1 gives Y = Y + 1
- –=(减法和赋值运算符): 它从左侧值中减去右侧值,然后将结果赋给左侧操作数。 例子:
Y -= 1 gives Y = Y - 1
- 同样,也有 *=(乘法和赋值) , /=(除法和赋值) , %=(模块和作业) 例子:
Y *= A is equivalent to Y = Y * AY /= A is equivalent to Y = Y / AY %= A is equivalent to Y = Y % A
比较运算符: JavaScript中有各种比较运算符——
- = = : 比较两个操作数的相等性。如果相等,则条件为真,否则为假。 例子:
Y = 5 and X = 6 Y = = X is false.
- 注: “==”运算符中未考虑类型。
- = = = : 此运算符将两个操作数的相等性与类型进行比较。如果相等(类型和值都相等),则条件为真,否则为假。 例子:
given X = 10 then X = = = "10" is false.X = = = 10 is true.
- != (不平等): 比较两个操作数的不等式。如果操作数不相等,则为True。 例子:
given X = 10 then X ! = 11 is true.
- >(大于): 该操作员检查左侧值是否大于右侧值。如果是,则返回true,否则返回false。 例子:
given X = 10 then X > 11 is false.
- 该操作员检查左侧值是否小于右侧值。如果是,则返回true,否则返回false。 例子:
given X = 10 then X < 11 is true.
- >=(大于或等于): 此运算符检查左侧操作数是否大于或等于右侧操作数。如果是,则返回true,否则返回false。 例子:
given X = 10 then X > = 11 is false.
- <=(小于或等于): 此运算符检查左侧操作数值是否小于或等于右侧操作数值。如果是,则返回true,否则返回false。 例子:
given X = 10 then X < = 10 is true.
逻辑运算符: JavaScript中有各种逻辑运算符——
- &&(逻辑与): 它检查两个操作数是否为非零(0、false、undefined、null或“”被视为零),如果是,则返回1,否则返回0。 例子:
Y = 5 and X = 6Y && X is true.
- ||(逻辑或): 它检查两个操作数中是否有任何一个非零(0、false、undefined、null或“”被视为零)。因此| |如果其中一个操作数为真,则返回真;如果两个操作数都为假,则返回假。 例子:
Y = 5 and X = 0Y || X is true.
- ! (逻辑上不是): 它反转操作数(或条件)的布尔结果。 例子:
Y = 5 and X = 0!(Y || X) is false.
三元运算符:
- : ? 接线员: 这就像if-else条件的缩写形式。 语法:
Y = ? A : B
- 其中A和B是值,如果条件为真,则Y=A,否则Y=B。 例子:
Y = (6>5) ? 6 : 5therefore Y = 6
- 操作员类型: 它返回变量的类型。 语法:
typeof variable;
例子:
javascript
<html> <head> <title> GfG typeof example </title> </head> <body> <script type= "text/javascript" > var a = 17; var b = "GeeksforGeeks" ; var c = "" ; var d = null ; document.write( "Type of a = " + ( typeof a)); document.write( "<br>" ); document.write( "Type of b = " + ( typeof b)); document.write( "<br>" ); document.write( "Type of c = " + ( typeof c)); document.write( "<br>" ); document.write( "Type of d = " + ( typeof d)); document.write( "<br>" ); document.write( "Type of e = " + ( typeof e)); document.write( "<br>" ); </script> </body> </html> |
输出:
Type of a = numberType of b = stringType of c = stringType of d = objectType of e = undefined
JavaScript最为人所知的是网页开发,但它也用于各种非浏览器环境。通过以下步骤,您可以从头开始学习JavaScript JavaScript教程 和 JavaScript示例 .
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END