0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

はじめに

こんにちは。
今回は、JavaScriptにおける文・式・式文についてまとめます。

式は値になるもの

式(Expression)は、評価すると値になる(結果がある)ものです。
たとえば、次のコードの 1 + 2"hello" は式です。

const x = 1 + 2;
console.log("hello");

文:処理の1ステップ

文(Statement)は処理の1ステップのことです。文には結果(値)がなく、式と違って const x = 文; のような書き方はできません。

次のコードでは、const age = 20; は変数を宣言する文、if (...) { ... } は条件によって処理を分岐する文です。

const age = 20;

if (age >= 20) {
  console.log("adult");
}

式文:式を文として書いたもの

式文は式を文として書いたものです。式は文として書くことができますが、文を式として書くことはできません。

console.log("hello");

const age = 20;
age + 1;

このコードでは、本来は式である console.log("hello") を文として書いている式文です。
また、age + 1; も式文ですが、計算結果をどこにも代入していないため、評価結果は捨てられてしまいます。

まとめ

  • は、評価すると値になるもの
  • は、宣言や制御構文など、処理の1ステップになるもの
  • 式文は、式を文として書いたもの

お読みいただきありがとうございました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?