Simple mastery of the use of Const declaration constants and variables in JavaScript basics

Source: Internet
Author: User

The third declaration class 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 can't assign a value to a C2, but you can change the contents of C2 because C2 is an object, see example:

C2.P1 = 1; 

Also, you can add elements to the C3 because C3 is a number of arrays.

The const declaration constant also has the problem that the declaration and initialization must be together and declared to be initialized:

' Use strict '; 
 
Const C4;//uncaught syntaxerror:unexpected token; 

Remove the semicolon will still be an error, we do not discuss the situation under strict mode, interested in the can go to try.

The variables of the const declaration are similar to those of let declarations, and they differ in that the const declared variable can only be assigned at the time of declaration and cannot be modified arbitrarily, otherwise it can cause syntaxerror (syntax error).

Const MAX_CAT_SIZE_KG = 3000; Correct

max_cat_size_kg = 5000;//Syntax error (SYNTAXERROR)
max_cat_size_kg++;//Though changed in one way, it still causes grammatical errors

Of course, the specification design is wise enough to declare a variable with a const must be assigned a value, or else throw a syntax error.

Const Thefairest; It's still a grammatical mistake, you miserable bastard.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.