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?

定数について

Last updated at Posted at 2025-03-15

変数とよく似たものに、定数があります。
定数はletの代わりにconstを用いて定義します。変数と定数の違いについて学習していきましょう。

image.png

定数と変数の違い

変数と定数の違いを見てみましょう。
左の図のように、変数は一度代入した値を更新することができました。
しかし、右の図のように定数は値を更新することはできません。 定数の値を更新しようとすると、コードを実行した際にエラーが発生してしまうので気をつけましょう。
image.png

定数を使うメリット
定数のメリットは、「後から値を更新できない」ことにあります。
これは一見不便に感じるかもしれませんが、予期せぬ更新を防ぐことができ、より安全なコードを書くことができます。
コードの量が増えるにつれて、このメリットを実感できるようになります。

image.png

例題

以下の例題に取り組んでみましょう。

//定数languageを定義してください

//定数languageの値を出力してください

//定数languageを用いて、「〇〇を話せます」と出力してください            
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?