Show 例子 2
优质
小牛编辑
147浏览
2023-12-01
JavaScript支持以下比较运算符。 假设变量A保持10 ,变量B保持20 ,则 -
Sr.No | 运算符和描述 | 例 |
---|---|---|
1 | = = (Equal) 检查两个操作数的值是否相等,如果是,则条件成立。 | (A == B)不是真的。 |
2 | != (Not Equal) 检查两个操作数的值是否相等,如果值不相等,则条件变为true。 | (A!= B)是真的。 |
3 | 》 (Greater than) 检查左操作数的值是否大于右操作数的值,如果是,则条件变为真。 | (A> B)不是真的。 |
4 | 《 (Less than) 检查左操作数的值是否小于右操作数的值,如果是,则条件变为真。 | (A < B) 为真 |
5 | 》= (Greater than or Equal to) 检查左操作数的值是否大于或等于右操作数的值,如果是,则条件变为真。 | (A> = B)不是真的。 |
6 | 《= (Less than or Equal to) 检查左操作数的值是否小于或等于右操作数的值,如果是,则条件变为真。 | (A <= B)是真的。 |
例子 (Example)
以下代码显示了如何在CoffeeScript中使用比较运算符。 将此代码保存在名为comparison_example.coffee的文件中
a = 10
b = 20
console.log "The result of (a == b) is "
result = a == b
console.log result
console.log "The result of (a < b) is "
result = a < b
console.log result
console.log "The result of (a > b) is "
result = a > b
console.log result
console.log "The result of (a != b) is "
result = a != b
console.log result
console.log "The result of (a >= b) is "
result = a <= b
console.log result
console.log "The result of (a <= b) is "
result = a >= b
console.log result
打开command prompt并编译comparison_example.coffee文件,如下所示。
c:/> coffee -c comparison_example.coffee
在编译时,它为您提供以下JavaScript。
// Generated by CoffeeScript 1.10.0
(function() {
var a, b, result;
a = 10;
b = 20;
console.log("The result of (a == b) is ");
result = a === b;
console.log(result);
console.log("The result of (a < b) is ");
result = a < b;
console.log(result);
console.log("The result of (a > b) is ");
result = a > b;
console.log(result);
console.log("The result of (a != b) is ");
result = a !== b;
console.log(result);
console.log("The result of (a >= b) is ");
result = a <= b;
console.log(result);
console.log("The result of (a <= b) is ");
result = a >= b;
console.log(result);
}).call(this);
现在,再次打开command prompt并运行CoffeeScript文件,如下所示。
c:/> coffee comparison_example.coffee
执行时,CoffeeScript文件生成以下输出。
The result of (a == b) is
false
The result of (a < b) is
true
The result of (a > b) is
false
The result of (a != b) is
true
The result of (a >= b) is
true
The result of (a <= b) is
false