Coffeescript-if-then-statement-in-coffeescript

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

CoffeeScript-if-thenステートメント

*if-then* ステートメントを使用して、CoffeeScriptの *if* ステートメントを1行で記述できます。 ブール式の後にthenキーワードが続き、その後に1つ以上のステートメントが続きます。 これらのステートメントは、指定されたブール式が真のときに実行されます。

構文

以下は、CoffeeScriptの if-then ステートメントの構文です。

if expression then Statement(s) to be executed if expression is true

以下は、CoffeeScriptの if-then ステートメントの例です。 このコードを if_then_example.coffee という名前のファイルに保存します

name = "Ramu"
score = 60
if score>40 then console.log "Congratulations you have passed the examination"
  • コマンドプロンプト*を開き、以下に示すように.coffeeファイルをコンパイルします。
c:\> coffee -c if_then_example.coffee

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

//Generated by CoffeeScript 1.10.0
(function() {
  var name, score;

  name = "Ramu";

  score = 60;

  if (score > 40) {
    console.log("Congratulations you have passed the examination");
  }

}).call(this);

ここで、*コマンドプロンプト*を再度開き、CoffeeScriptファイルを次のように実行します-

c:\> coffee if_then_example.coffee

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

Congratulations you have passed the exam