| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Statements/const | [Back] [Original] |
Get to know MDN better
const number = 42;
try {
number = 99;
} catch (err) {
console.log(err);
// : TypeError: invalid assignment to const 'number'
//
}
console.log(number);
// : 42
const name1 = value1;
const name1 = value1, name2 = value2;
const name1 = value1, name2 = value2, /* , */ nameN = valueN;
const let
const
const globalThis
if (true) const a = 1; // SyntaxError: Lexical declaration cannot appear in a single-statement context
const FOO; // SyntaxError: Missing initializer in const declaration
// MY_FAV 7
const MY_FAV = 7;
console.log(`my favorite number is: ${MY_FAV}`);
//
MY_FAV = 20; // TypeError: Assignment to constant variable
//
const MY_FAV = 20; // SyntaxError: Identifier 'MY_FAV' has already been declared
var MY_FAV = 20; // SyntaxError: Identifier 'MY_FAV' has already been declared
let MY_FAV = 20; // SyntaxError: Identifier 'MY_FAV' has already been declared
const MY_FAV = 7;
if (MY_FAV === 7) {
// MY_FAV
const MY_FAV = 20;
console.log(MY_FAV); // 20
//
var MY_FAV = 20; // SyntaxError: Identifier 'MY_FAV' has already been declared
}
console.log(MY_FAV); // 7
const "Assignment to constant variable"
const MY_OBJECT = { key: "value" };
MY_OBJECT = { OTHER_KEY: "value" };
MY_OBJECT.key = "otherValue";
"Assignment to constant variable"
const MY_ARRAY = [];
MY_ARRAY = ["B"];
MY_ARRAY.push("A"); // ["A"]
=
const result = /(a+)(b+)(c+)/.exec("aaabcc");
const [, a, b, c] = result;
console.log(a, b, c); // "aaa" "b" "cc"
| ECMAScript 2027 LanguageSpecification # sec-let-and-const-declarations |
| Web Proxy Viewer | New URL | Original Page |