Const

The const keyword is a way for you to convey that this data should never be changed. As a code base grows, it is easy that changes happen by mistake; such a mistake might be costly. To get compile time support for this, the const keyword is there to help you. Use it in the following way:

const PI = 3.14;
PI = 3 // not allowed

The compiler will even indicate that this is not allowed with the following message:

Cannot assign to PI because it is a constant or a read-only property

A word of caution here: this works only on the top level. You need to be aware of this if you declare objects as const, like so:

const obj = {
a : 3
}
obj.a = 4; // actually allowed

Declaring the obj const does not freeze the entire object from being edited, but rather what obj points to. So, the following would not be allowed:

obj = {}

Here, we actively change what obj points to, not one of its child properties, therefore it is not allowed and you get the same compile error as earlier.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset