JavaScript比较运算符用法和介绍指南

下面是比较运算符的示例。
范例1:

< script> function gfg() { let val1 = 5; // Equality Operators document.write(val1 == 5); document.write( "< br> " ); // Relational Operators document.write(val1 > 0); } gfg(); < /script>

输出如下:
true true

操作符用于对操作数执行特定的数学和逻辑计算。像C, C ++, Java, Python和其他各种语言一样, JavaScript也支持比较方式操作。比较运算符在逻辑语句中用于确定变量或值之间的相等性或差异。 JavaScript支持各种比较运算符:
  • 平等经营者
  • 关系运算符
平等经营者
平等(==):该运算符用于比较两个操作数的相等性。如果相等, 则条件为true, 否则为false。
语法如下:
x == y

以下示例说明了(==)JavaScript中的运算符:
范例1:
< script> // Illustration of (==) operator let val1 = 5; let val2 = '5' ; // Checking of operands console.log(val1 == 5); console.log(val2 == 5); console.log(val1 == val1); // Check against null and boolean value console.log(0 == false ); console.log(0 == null ); < /script>

输出如下:
> true > true > true > true > false

范例2:
< script> // Illustration of (==) operator let obj1 = { 'val1' : 'value' }; let obj2 = { 'val2' : 'value' }; // Checking of operands console.log(obj1.val1 == 'value' ); console.log(obj1 == obj2); console.log(obj1.val1 == obj2.val2); // Check against undefined console.log(0 == undefined); console.log( null == undefined); < /script>

输出如下:
> true > false > true > false > true

一世平等(!=):该运算符用于应对两个操作数的不等式。如果相等, 则条件为假, 否则为真。
语法如下:
x != y

以下示例说明了(!=)操作符inJavaScript:
范例1:
< script> // Illustration of (!=) operator let val1 = 5; let val2 = '5' ; // Checking of operands console.log(val1 != 6); console.log(val2 != '5' ); console.log(val1 != val1); // Check against null and boolean value console.log(0 != false ); console.log(0 != null ); < /script>

输出如下:
> true > false > false > false > true

范例2:
< script> // Illustration of (!=) operator let obj1 = { 'val1' : 'value' }; let obj2 = { 'val2' : 'value' }; // Checking of operands console.log(obj1.val1 != 'value' ); console.log(obj1 != obj2); console.log(obj1.val1 != obj2.val2); // Check against undefined console.log(0 != undefined); console.log( null != undefined); < /script>

输出如下:
> false > true > false > true > false

严格平等(===):该运算符用于要求两个操作数的类型相等。如果值和类型都相等, 则条件为true, 否则为false。
语法如下:
x === y

以下示例说明了(===)操作符inJavaScript:
范例1:
< script> // Illustration of (===) operator let val1 = 5; let val2 = '5' ; // Checking of operands console.log(val1 === 6); console.log(val2 === '5' ); console.log(val1 === val1); // Check against null and boolean value console.log(0 === false ); console.log(0 === null ); < /script>

输出如下:
> false > true > true > false > false

范例2:
< script> // Illustration of (===) operator let obj1 = { 'val1' : 'value' }; let obj2 = { 'val2' : 'value' }; // Checking of operands console.log(obj1.val1 === 'value' ); console.log(obj1 === obj2); console.log(obj1.val1 === obj2.val2); // Check against undefined console.log(0 === undefined); console.log( null === undefined); < /script>

输出如下:
> true > false > true > false > false

严格的平等(!==):该运算符用于ompare两个具有类型的操作数的不等式。如果值和类型都不相等, 则条件为true, 否则为false。
语法如下:
x !== y

以下示例说明了(!==)操作符inJavaScript:
范例1:
< script> // Illustration of (!==) operator let val1 = 5; let val2 = '5' ; // Checking of operands console.log(val1 !== 6); console.log(val2 !== '5' ); console.log(val1 !== val1); // Check against null and boolean value console.log(0 !== false ); console.log(0 !== null ); < /script>

输出如下:
> true > false > false > true > true

范例2:
< script> // Illustration of (!==) operator let obj1 = { 'val1' : 'value' }; let obj2 = { 'val2' : 'value' }; // Checking of operands console.log(obj1.val1 !== 'value' ); console.log(obj1 !== obj2); console.log(obj1.val1 !== obj2.val2); // Check against undefined console.log(0 !== undefined); console.log( null !== undefined); < /script>

输出如下:
> false > true > false > true > true

关系运算符G胜于运算符(> ):这个运算符习惯于检查左侧值是否大于右侧值。如果值较大, 则条件为true, 否则为false。
语法如下:
x > y

以下示例说明了(> )操作符inJavaScript:
范例1:
< script> // Illustration of (> ) operator let val1 = 5; let val2 = "5" ; // Checking of operands console.log(val1 > 0); console.log(val2 > "10" ); console.log(val1 > "10" ); console.log(val2 > 0); < /script>

输出如下:
> true > true > false > true

范例2:
< script> // Illustration of (> ) operator let obj1 = { 'val1' : 1}; let obj2 = { 'val2' : 3}; // Checking of operands console.log(obj1.val1 > 0); console.log(obj1 > obj2); console.log(obj1.val1 > obj2.val2); console.log(obj2 > obj1); console.log(obj2.val2 > obj1.val1); < /script>

输出如下:
> true > false > false > false > true

G大于或等于运算符(> =):这个运算符习惯于检查左侧操作数是否大于或等于右侧操作数。如果值是g大于或等于那么条件为真, 否则为假。
语法如下:
x > = y

以下示例说明了(> =)操作符inJavaScript:
范例1:
< script> // Illustration of (> =) operator let val1 = 5; let val2 = "5" ; // Checking of operands console.log(val1 > = 5); console.log(val2 > = "15" ); console.log(val1 > = "5" ); console.log(val2 > = 15); < /script>

输出如下:
> true > true > true > false

范例2:
< script> // Illustration of (> =) operator let obj1 = { 'val1' : 1}; let obj2 = { 'val2' : 3}; // Checking of operands console.log(obj1.val1 > = 0); console.log(obj1 > = obj2); console.log(obj1.val1 > = obj2.val2); console.log(obj2 > = obj1); console.log(obj2.val2 > = obj1.val1); < /script>

输出如下:
> true > true > false > true > true

大号比操作员重要(< ):这个运算符习惯于检查左侧值是否小于右侧值。如果是, 则条件为真, 否则为假。
语法如下:
x < y

以下示例说明了(< )操作符inJavaScript:
范例1:
< script> // Illustration of (< ) operator let val1 = 5; let val2 = "5" ; // Checking of operands console.log(val1 < 15); console.log(val2 < "0" ); console.log(val1 < "0" ); console.log(val2 < 15); < /script>

输出如下:
> true > false > false > true

范例2:
< script> // Illustration of (< ) operator let obj1 = { 'val1' : 1}; let obj2 = { 'val2' : 3}; // Checking of operands console.log(obj1.val1 < 10); console.log(obj1 < obj2); console.log(obj1.val1 < obj2.val2); console.log(obj2 < obj1); console.log(obj2.val2 < obj1.val1); < /script>

输出如下:
> true > false > true > false > false

大号等于或小于等于运算符(< =):该运算符用于检查左侧操作数的值是否小于或等于右侧操作数的值。如果是, 则条件为真, 否则为假。
语法如下:
x < = y

以下示例说明了(< =)操作符inJavaScript:
范例1:
< script> // Illustration of (< =) operator let val1 = 5; let val2 = "5" ; // Checking of operands console.log(val1 < = 15); console.log(val2 < = "0" ); console.log(val1 < = "0" ); console.log(val2 < = 15); < /script>

输出如下:
> true > false > false > true

范例2:
< script> // Illustration of (< =) operator let obj1 = { 'val1' : 1}; let obj2 = { 'val2' : 3}; // Checking of operands console.log(obj1.val1 < = 10); console.log(obj1 < = obj2); console.log(obj1.val1 < = obj2.val2); console.log(obj2 < = obj1); console.log(obj2.val2 < = obj1.val1); < /script>

输出如下:
> true > true > true > true > false

【JavaScript比较运算符用法和介绍指南】支持的浏览器:所有人都支持的浏览器JavaScript比较运算符下面列出:
  • 谷歌浏览器
  • 火狐浏览器
  • 歌剧
  • 苹果浏览器
  • Edge
  • IE浏览器

    推荐阅读