[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Statements/const [Back]  [Original]

const - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

const

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 7.

const . .

In this article

const number = 42;

try {
  number = 99;
} catch (err) {
  console.log(err);
  // Expected output: TypeError: invalid assignment to const 'number'
  // (Note: the exact output may be browser-dependent)
}

console.log(number);
// Expected output: 42

js
    const name1 = value1 [, name2 = value2 [, ... [, nameN = valueN]]];
nameN

. .

valueN

. .

. (initializer) . ( ).

let (block-scope). .

let " " , const .

.

. .

js
// :       ,
//      .

// MY_FAV     7 
const MY_FAV = 7;

//  
MY_FAV = 20;

// 7 
console.log("my favorite number is: " + MY_FAV);

//      - Uncaught SyntaxError: Identifier 'MY_FAV' has already been declared
const MY_FAV = 20;

// MY_FAV       .
var MY_FAV = 20;

//   
let MY_FAV = 20;

//     
if (MY_FAV === 7) {
  //    MY_FAV    
  // (let const        )
  let MY_FAV = 20;

  // MY_FAV  20
  console.log("my favorite number is " + MY_FAV);

  //      .
  var MY_FAV = 20;
}

// MY_FAV  7
console.log("my favorite number is " + MY_FAV);

// const     
const FOO;

// const   
const MY_OBJECT = { key: "value" };

//    
MY_OBJECT = { OTHER_KEY: "value" };

//     .
//     
MY_OBJECT.key = "otherValue"; //      Object.freeze()   

//   
const MY_ARRAY = [];
//     
MY_ARRAY.push("A"); // ["A"]
//       
MY_ARRAY = ["B"];

Specification
ECMAScript 2027 LanguageSpecification
# sec-let-and-const-declarations


Web Proxy Viewer  |  New URL  |  Original Page