const

ืžื™ืœืช ื”ื”ืฆื”ืจื” const ืžืฉืžืฉืช ืœื”ื›ื–ืจื” ืขืœ ืžืฉืชื ื” ืงื‘ื•ืข ืฉืื™ืŸ ืืคืฉืจื•ืช ืœืฉื ื•ืช ืืช ื”ืขืจืš ืฉืœื•.
 

ืชื—ื‘ื™ืจ

const name1 = value1 [, name2 = value2 [, ... [, nameN = valueN]]];
nameN
ืฉื ื”ืžืฉืชื ื”.
valueN
ื”ืขืจืš ืฉืœ ื”ืžืฉืชื ื”.

ืชื™ืื•ืจ

ื‘ืฉืคืช ื’'ืื•ื•ื” ืกืงืจื™ืคื˜ ืื ื• ืžืฉืชืžืฉื™ื ื‘ืžืฉืชื ื™ื ืขืœ ืžื ืช ืœื”ื—ื–ื™ืง ืขืจื›ื™ื ืฉื•ื ื™ื.
ื”ืฆื”ืจื” ืขืœ ืžืฉืชื ื” ื‘ืืžืฆืขื•ืช const ื”ื•ืคื›ืช ืื•ืชื• ืœืงื‘ื•ืข ื•ืœื ื ื™ืชืŸ ืœืฉื ื•ืช ืืช ื”ืขืจืš ืฉืœื•.
ื”ืงืฆืืช ืขืจืš ืœืžืฉืชื ื” ืœืœื ื”ืฆื”ืจื” ืžืจืืฉ ื”ื•ืคื›ืช ืื•ืชื• ืœืžืฉืชื ื” ื’ืœื•ื‘ืœื™, ืืš ื‘ืฉื•ื ื” ืžื”ืฆื”ืจื” ื‘ืืžืฆืขื•ืช var ื”ื•ื ืื™ื ื• ื›ืคื•ืฃ ืœืื•ื‘ื™ื™ืงื˜ ื”ืื‘ window.
ื—ืฉื•ื‘ ืœืฆื™ื™ืŸ ืฉื”ืฆื”ืจื” ื‘ืืžืฆืขื•ืช const ืœื ืžื‘ืฆืขืช Hoisting.

ื“ื•ื’ืžืื•ืช

ื”ื“ื•ื’ืžื” ื”ื‘ืื” ืžืžื—ื™ืฉื” ื›ื™ืฆื“ ืžืชื ื”ื’ื™ื ืžืฉืชื ื™ื ืงื‘ื•ืขื™ื.

// NOTE: Constants can be declared with uppercase or lowercase, but a common
// convention is to use all-uppercase letters.

// define MY_FAV as a constant and give it the value 7
const MY_FAV = 7;

// this will throw an error - Uncaught TypeError: Assignment to constant variable.
MY_FAV = 20;

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

// trying to redeclare a constant throws an error -  Uncaught SyntaxError: Identifier 'MY_FAV' has already been declared
const MY_FAV = 20;

// the name MY_FAV is reserved for constant above, so this will fail too
var MY_FAV = 20;

// this throws an error too
let MY_FAV = 20;

// it's important to note the nature of block scoping
if (MY_FAV === 7) {
    // this is fine and creates a block scoped MY_FAV variable
    // (works equally well with let to declare a block scoped non const variable)
    let MY_FAV = 20;

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

    // this gets hoisted into the global context and throws an error
    var MY_FAV = 20;
}

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

// throws an error - Uncaught SyntaxError: Missing initializer in const declaration
const FOO;

// const also works on objects
const MY_OBJECT = {'key': 'value'};

// Attempting to overwrite the object throws an error - Uncaught TypeError: Assignment to constant variable.
MY_OBJECT = {'OTHER_KEY': 'value'};

// However, object keys are not protected,
// so the following statement is executed without problem
MY_OBJECT.key = 'otherValue'; // Use Object.freeze() to make object immutable

// The same applies to arrays
const MY_ARRAY = [];
// It's possible to push items into the array
MY_ARRAY.push('A'); // ["A"]
// However, assigning a new array to the variable throws an error - Uncaught TypeError: Assignment to constant variable.
MY_ARRAY = ['B'];

ืžืคืจื˜

ืชืื™ืžื•ืช ื“ืคื“ืคืŸ

BCD tables only load in the browser

ืจืื” ื’ื