preface

In ES6, you can declare a variable using either let or const, except that the latter declares an object with an immutable memory address.

Basic usage

Take a look at the following code to get a feel for itconstThe use of:The above code shows that changing the value of a constant will cause an error. If a simple type of data declared by const is used in the code,Numeric value, string, Boolean value) its value is immutable, which is a bit like the final modifier in Java, where the base type variable is a constant. One thing to noteconstwithletBoth are valid only at the declared block-level scope.

        {
            const b =123;
        }
        // Uncaught ReferenceError: b is not defined
        console.log(b)
Copy the code

When usingconstWhen an object and array are declared, the reference to the variable cannot be modified, but the internal properties of the variable can be modified.Define an object personConsole output:We are defining an array object:Console output:In the above code, the const variable holds an address that points to an object reference. Const guarantees that the address value is immutable, but the object itself is mutable, so properties inside the object can be changed.