Coffeescript-comparison-operators

提供:Dev Guides
移動先:案内検索

CoffeeScript-比較演算子

JavaScriptは、次の比較演算子をサポートしています。 変数 A10 を保持し、変数 B20 を保持すると仮定します-

Sr.No Operator and Description Example
1

= = (Equal)

2つのオペランドの値が等しいかどうかを確認し、等しい場合は条件が真になります。

(A == B) is not true.
2

!= (Not Equal)

2つのオペランドの値が等しいかどうかをチェックし、値が等しくない場合は条件が真になります。

(A != B) is true.
3

> (Greater than)

左のオペランドの値が右のオペランドの値よりも大きいかどうかをチェックし、そうであれば条件は真になります。

(A > B) is not true.
4

< (Less than)

左のオペランドの値が右のオペランドの値よりも小さいかどうかを確認し、そうであれば条件は真になります。

(A < B) is true.
5

>= (Greater than or Equal to)

左のオペランドの値が右のオペランドの値以上かどうかをチェックし、そうであれば条件は真になります。

(A >= B) is not true.
6

⇐ (Less than or Equal to)

左のオペランドの値が右のオペランドの値以下かどうかをチェックし、そうであれば条件は真になります。

(A ⇐ B) is true.

次のコードは、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
  • コマンドプロンプト*を開き、以下に示すように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);

次に、*コマンドプロンプト*を再度開き、以下に示すように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