Like many other programming languages, JavaScript is also written in text format, which consists of statements (statements), statement blocks (blocks), and comments. A block is a set of statements that are composed of correlated statements. In a statement, you can use variables, strings, numbers (literals), and expressions (expressions ).
Statement (statements)
A JavaScript program is a set of statements. A JavaScript statement is equivalent to a complete sentence. Javascript statements combine expressions in some way to complete a task.
A statement contains one or more expressions, keywords, and operators ). Generally, all the content of a statement is written in the same row. However, a statement can also be written into multiple rows. In addition, multiple statements can be separated by semicolons (;) and written in the same row.
Suggestion: end each statement in the form of display, that is, the last plus sign (;) of each statement indicates the end of the statement.
The following are examples of statements:
Abird = "Robin"; the above statement indicates assigning the "Robin" string to the variable abird.
VaR today = new date (); The preceding statement assigns the value of today's date to the variable today.
Statement block (blocks)
Generally, a set of JavaScript statements enclosed by {} are called statement blocks (blocks ). Statement blocks can be considered as separate statements. That is to say, in many cases, statement blocks can be called by other JavaScript code as a single statement. However, loop statements starting with for and while are not supported. Note that,
Note: Each statement in the statement block ends with a semicolon (;), but the statement block itself does not use a semicolon.
Statement blocks are usually used in functions and condition statements.
In the example below, the five sentences in the middle of {} constitute a block, and the last three statements are not in the block.
Function convert (INCHES) {feet = inches/12; miles = feet/5280; nauticalmiles = feet/6080; CM = inches * 2.54; meters = inches/39.37 ;} km = meters/1000; kradius = km; mradius = miles; Comments)
You can write comments for codes in javascript programs to make the program readable and facilitate code modification and maintenance in the future ).
In JavaScript, two slashes are used to represent single-line comments. See example:
Agoodidea = "comment your code thoroughly."; // This is a single line comment. For multi-line comments, start is indicated by/*, and end is indicated. See example:
/* This is multi-line comment Line 1. This is multi-line comment Row 2. */We recommend that you replace multiple-line comments with single-line comments, which helps distinguish code from comments.
Expression (expressions)
Javascript expressions are equivalent to a phrase in Javascript. This phrase can be used to determine or generate a value, which can be any legal JavaScript type-numbers, strings, objects, etc. The simplest expression is a character.
Expression example:
3.9 // numeric character "Hello! "// String character false // Boolean character null // null value character {X: 1, Y: 2} // object character [1, 2, 3] // array character function (x) {return x * X;} // The following is a complex expression example:
VaR anexpression = 3 * (4/5) + 6; var asecondexpression = math. pI * radius; var athirdexpression = asecondexpression + "%" + anexpression; var afourthexpression = "(" + asecondexpression + ") % (" + anexpression + ")"; assignments and equality)
In JavaScript, equal sign (=) is used to assign values to variables. The value on the left of the equal sign can be:
Variable
Array Element
Object Attributes
The value on the right of the equal sign can be any type of value, including the expression. The example is as follows, which indicates that the integer 8 is assigned to the variable X.
X = 8;
Note: In JavaScript, You need to determine whether the two values are equal. Instead of equal signs, they are expressed by equal signs (= ). For example, X equals to 8.
X = 8