2
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-08-19

はじめに

デフォルト値について学んだので基本的な使い方について記述いたします。

デフォルト値

const sayHello = (name) => console.log(`こんにちは!${name}です。`);
sayHello();
console

こんにちは!undefinedです。

javascriptの変数はなにも設定されていないと「undefined」という文字列が出力されます。
上記のような値が渡されていないときでも、ある程度の挙動をした文字列を出力したいときにデフォルト値を使います。

デフォルト値

const sayHello = (name = "ゲスト") => console.log(`こんにちは!${name}です。`);
sayHello(); 
console

こんにちは!ゲストです。

値が渡されていないときのために、あらかじめ引数のnameに値を用意しておくものがデフォルト値になります。値が設定されると元の関数のように使えるのが便利な利点です。

const myprofile = {
  age: 26,
};
const { age, name = "ゲスト" } = myprofile;
console.log(age);
console.log(name);
console

26
ゲスト

分割代入でも使用可能で上記のようにデフォルト値を用意することが可能です。

おわりに

オブジェクトから分割代入をするときのデフォルト値の設定はよく使用されるので、使えるように頑張りましょう。

JISOUのメンバー募集中!

プログラミングコーチングJISOUでは、新たなメンバーを募集しています。
日本一のアウトプットコミュニティでキャリアアップしませんか?
興味のある方は、ぜひホームページをのぞいてみてください!

2
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
2
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?