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?

JavaScript let const

Last updated at Posted at 2025-12-09

letとconstの違い

JavaScriptにおけるletとconstはどちらも変数を宣言するキーワードで、再代入の可否とスコープが違う

const

  • 値の変更ができない
  • 変数を宣言するときに必ず初期化が必要
const API_KEY = "abcdefg";
// API_KEY = "new_key";など再代入禁止

let

  • 値の再代入が可能
  • 宣言時に初期値がなくてもいい
let count = 0;
count = 1; // ✅ 再代入OK!

let message; // 初期値なしで宣言OK
message = "Hello";

スコープ範囲

  • const,let共に宣言された{}ブロック内でのみ有効
function getCount() {
  let result = 0;
  
  if (true) {
    const limit = 10; // 👈 ブロックスコープ
    result = limit;
  }
  
  // console.log(limit); // ❌ エラー: limit is not defined
  return result;
}
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?