0
2

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の変数定義に関して(var,let,constの使い分け)

Posted at

①再代入、再定義可能か
 let,varは可能
 constは不可能(※定数だから)

②スコープの範囲
var・・・関数スコープ

function test() {
  if (true) {
    //条件式の中で宣言
    var x = 1;
  }

  console.log(x) // 条件式の外で呼び出しても、関数内なので1が表示される
}

let,const・・・ブロックスコープ

function test() {
  //if文の{}が1つのブロックなので、
  if (true) {
    //条件式の中で宣言すると
    let x = 1;
    const y = 2;
  }

  console.log(x) //ブロックの外側なのでundefined(未定義)となる
  console.log(y) //constを使った場合も同様にundefinedとなる
};
0
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?