LoginSignup
1
1

More than 3 years have passed since last update.

JavaScriptの変数宣言 let,ver,const

Last updated at Posted at 2020-11-24

何度調べても覚えられないのでざっくりまとめ

結論

const

定数
一度入力した値は変更されない

let

ローカル変数
一度入力した値を変更できる

var

グローバル変数
いつでもどこでも変更可能
なるべく使わない

使い方

上に書いたやつ優先で使う
constが最優先で、次点がlet

使用例

雰囲気で感じろ

Sample.js
if(true){
    const hogeConst = "hoge1";
    let hogeLet     = "hoge2";
    var hogeVar     = "hoge3";

    console.log(hogeConst); // hoge1
    console.log(hogeLet);   // hoge2
    console.log(hogeVar);   // hoge3

    hogeConst = "hoge4" // エラー
    hogeLet   = "hoge5" // hoge5
    hogeVar   = "hoge6" // hoge6

    console.log(hogeLet); // hoge5
    console.log(hogeVar); // hoge6
}

console.log(hogeConst); // エラー
console.log(hogeLet);   // エラー
console.log(hogeVar);   // hoge6

まとめ

値の変更 スコープ外からの参照
const 不可 不可
let 不可
var

スコープのところは、上の例で言うと、ifの中で書いたらifの中でしか使えないよってこと

参考文献

ここ見たら全部わかるよ
"巻き上げ"のところは上に書いてないから読んでおいた方がいいかも
https://developer.mozilla.org/ja/docs/Web/JavaScript/Guide/Grammar_and_Types

1
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
1
1