Coffeescript-logical-operators

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

CoffeeScript-論理演算子

CoffeeScriptは次の論理演算子をサポートしています。 変数 Atrue を保持し、変数 Bfalse を保持すると仮定します-

Sr.No Operator and Description Example
1

&& (Logical AND)

両方のオペランドが真の場合、条件は真になります。

(A && B) is false.
2 *

(Logical OR)*

2つのオペランドのいずれかが真の場合、条件は真になります。

(A
B) is true. 3

! (Logical NOT)

オペランドの論理状態を反転します。 条件が真の場合、Logical NOT演算子はそれを偽にします。

以下は、coffeeScriptでの論理演算子の使用を示す例です。 このコードを logical_example.coffee という名前のファイルに保存します。

a = true
b = false

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
  • コマンドプロンプト*を開き、以下に示すように.coffeeファイルをコンパイルします。
c:\> coffee -c logical_example.coffee

コンパイル時に、次のJavaScriptが提供されます。

//Generated by CoffeeScript 1.10.0
(function() {
  var a, b, result;
  a = true;
  b = false;

  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 logical_example.coffee

CoffeeScriptファイルを実行すると、次の出力が生成されます。

The result of (a && b) is
false
The result of (a || b) is
true
The result of !(a && b) is
true