[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ru/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 ..

, . (const) , , let.

In this article

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

. , .

valueN

. , , .

, , , . window, var-. ; ( , ).

const ( ), . , , , . , , .

, let const.

.

, const. JavaScript- .

js
// :        ,
//     .      
//  .

//  MY_FAV       7
const MY_FAV = 7;

//     - Uncaught TypeError: Assignment to constant variable.
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)
  const 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);

//  ,      - Uncaught SyntaxError: Missing initializer in const declaration
const FOO;

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

//        - Uncaught TypeError: Assignment to constant variable.
MY_OBJECT = { OTHER_KEY: "value" };

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

//      
const MY_ARRAY = [];
// ,     
MY_ARRAY.push("A"); // ["A"]
//       .    - Uncaught TypeError: Assignment to constant variable
MY_ARRAY = ["B"];

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


Web Proxy Viewer  |  New URL  |  Original Page