0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Javascript 備忘録3<変数について>

Last updated at Posted at 2021-04-10

■変数の宣言について
→Javascriptは「静的片付け言語」
→変数を宣言する時に、型まで宣言する必要がない

■andの応用的使い方

function hello(name) {
    // nameに値が入っている場合は、その値で、入っていない場合はAnyoneが返る
    // なぜなら、nameがfalseの場合には||の性質上、後ろのtrueとなる値が返却されるため
    // ただし、引数が0の場合trueとして評価されてしまうので注意すること
    name = name || 'Anyone';
    console.log(name);
}


let name;

// nameに値が入っていた場合に、hello関数を実行する
if (name) {
    console.log(name);
}
// 上のと同じ処理だが、1行で済む
name && hello(name);
0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?