JavaScript language constants and variables

Source: Internet
Author: User

JavaScript language constants and variables
In the previous chapter, we will introduce how to use JavaScript to compile a HelloJS applet, where variables are used. Constants and variables are important components of expressions.
Constant
When declaring and initializing variables, you can add the keyword const before the identifier to specify this as a constant. As the name suggests, a constant does not change its value during use. The instance code is as follows:
Constnum = 100;
The NUM identifier is a constant and can only be assigned a value during initialization. We cannot assign a value to NUM again.
Variable
To declare a variable in JavaScript, add the keyword var before the identifier. The instance code is as follows:
Var scoreForStudent = 0.0;
This statement declares the scoreForStudent variable and is initialized to 0.0. If multiple variables are declared and initialized in a statement, all variables have the same data type:
Var x = 10, y = 20;
In the Declaration of multiple variables, we can also specify different data types:
Var x = 10, y = true;
X is an integer and y is a Boolean integer.


Naming rules
Good Programming habits when using constants and variables, their names must be standardized, so that the program is readable.
1. Constant name
Constants of the basic data type are all in uppercase. If they are composed of multiple words, they can be separated by underscores (_). For example:
Var YEAR = 60;
Var WEEK_OF_MONTH = 3;
2. variable name
The naming of variables has several styles, which are mainly clear and easy to understand. For convenience, some programmers use a single letter as the variable name, such as j and I, this will cause difficulties in program maintenance in the future, and the name of the variable will also increase. A single letter variable is generally used only for loop variables, because they act only in the loop body.
In the past, computer languages used to limit the length of variable names. However, computer languages do not have such restrictions. Therefore, we recommend that you use clear names to indicate the role of variables, generally, it starts with a lowercase letter and uses uppercase letters as the first letter at the beginning of each word. For example:
Var maximumNumberOfLoginAttempts = 10;
Var currentLoginAttempt = 0;
A name like this can be used to show the role of this variable at a glance.
In addition to the standard naming of constants and variables, the standard naming is also required for other language objects. The naming convention for objects and other types is generally that uppercase letters are used as the start and the first letter at the beginning of each word, for example, HelloWorldApp. The function name is often composed of multiple words. The first word is usually a verb and usually starts with a lowercase letter. The first letter starts with an uppercase letter. For example: balanceAccount and isButtonPressed.


2.1 annotations
JavaScript programs have two types of Annotations: single-line (//) and multi-line (/*... */). These annotation methods are similar to C, C ++, and Java.
1. Single Row comment
A single line comment can comment out a part of the whole line or a row. It is generally not used for comments of multiple consecutive lines. However, it can also be used to comment out code segments of multiple consecutive lines. The following are examples of style Annotations:

If x> 1 {// comment 1} else {return false; // Note 2} // if x> 1 {// Note 1 //} else {// return false; // comment 2 //}

2. Block comment
It is generally used for comments of multiple consecutive lines, but it can also be annotated by a single line. The following are examples of style Annotations:
If x> 1 {/* Comment 1 */} else {return false; /* Comment 2 */}/* if x> 1 {// comment 1} else {return false; // Note 2} * // * if x> 1 {/* Comment 1 */} else {return false;/* Comment 2 */}*/


Multi-line comments in JavaScript have the advantage of not having other languages, that is, they can be nested. The last case of the above example is that multi-line comments are nested.

It is necessary to use annotations in program code to annotate codes that are easy to misunderstand, but do not comment on codes that clearly express information. Note that frequent comments sometimes reflect the low quality of the Code. When you feel forced to add comments, consider rewriting the Code to make it clearer.

 

 

 

 

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.