The third declaration keyword introduced by ES6 is similar to let: const.
Take a look at the usage:
const c1 = 1; const c2 = {}; const c3 = []; Object.getOwnPropertyDescriptor(window,"c1") //Object {value: 1, writable: false, enumerable: true, configurable: false}The above example says that you cannot assign values to c2, but the content of c2 can be changed because c2 is an object. See the example:
c2.p1 = 1;
Similarly, you can also add elements to c3 because c3 is an array.
There is another problem with const declaring constants, that is, declaration and initialization must be together, and after declaration, it must be initialized:
'use strict'; const c4;//Uncaught SyntaxError: Unexpected token ;
If you remove the semicolon, you will still report an error. We will not discuss the situation under non-strict mode here. If you are interested, you can try it yourself.
The variables declared by const are similar to those declared by let. The difference is that the variables declared by const can only be assigned values during declaration and cannot be modified at will, otherwise it will cause SyntaxError (syntax error).
const MAX_CAT_SIZE_KG = 3000; // Correct MAX_CAT_SIZE_KG = 5000; // Syntax Error (SyntaxError) MAX_CAT_SIZE_KG++; // Although it has changed, it still causes syntax errors
Of course, the specification design is wise enough. After declaring variables with const, you must assign values, otherwise a syntax error will also be thrown.
const theFairest; // It's still a syntax error, you unlucky guy