This is the second day of my participation in the August More text Challenge. For details, see:August is more challenging
Hello everyone, I am the prosperity of the other shore 🌸, a front-end 👨🏻💻 that firmly believes that hard work can change destiny. If I have the honor to write an article that can get your favor, I am very lucky ~
This series of articles was first published in nuggets
Writing in the front
In this article, learn about lexical structures in JavaScript. Lexical structures are the basic rules of a development language that describe how programs should be written in the JavaScript language.
This article contains the following contents:
- The specification of the identifier
- Statements and expressions
- A semicolon
- annotation
- The code block
- Keywords and reserved words
identifier
An identifier is a legal name that identifies various values. The most common identifiers are variable names and function names. The JJavaScript language is case-sensitive, meaning that hello, hello, and hello are different identifiers.
Every language has its own set of rules for naming identifiers, and an error will be reported if an invalid identifier is encountered. In JavaScript, identifiers are named as follows:
- The first character must be any Unicode letter, or a dollar sign (
$
), underline (-
). - After the second character, you can use numbers unless the first rule is satisfied.
- Chinese variables are also supported in JavaScript, but are not recommended.
- Keywords and reserved words are also not allowed as identifiers
The example code is as follows:
var a = 1 / / legal
var$=true / / legal
var _ = 'Prosperity on the other side' / / legal
var a1 = '1' / / legal
var $_a = "Normal people don't write like this." / / legal
var 1a = 'Finish calves' // The first character of the variable cannot be a number
varProsperity on the other side = '/ / is legal
Copy the code
Statements and expressions
A statement in JavaScript is simply a line of code, such as:
console.log('Hello World')
Copy the code
Above is a statement that will print ‘Hello World’ in the named line. Normally, each statement in JavaScript gets its own line.
An expression in JavaScript is an expression used to get a return value, such as 1+2, which is an expression.
The difference between an expression and a statement is that a statement is used to perform some operation, while an expression is used to get a value.
Optional semicolon
Normally, the JavaScript language uses a semicolon (;) As the end of a statement, this is important for the readability and neatness of the JavaScript language.
The following example code shows using a semicolon as the end of a statement:
var sum = a + b;
var diff = a - b;
Copy the code
The sample code above can also be written as follows:
var sum = a + b;var diff = a - b;
Copy the code
From the above two examples, we can see that when you end a statement with a semicolon, it is allowed to write both statements on two or one line.
But semicolons are not necessary in the JavaScript language. If each statement in the JavaScript language has its own line, the ending semicolon can be omitted.
The following example code shows how to omit the semicolon:
var sum = a + b
var diff = a - b
Copy the code
Note, however, that the following example code will run with an error if the semicolon is omitted:
var sum = a + bvar diff = a - b;
Copy the code
The code block
In the JavaScript language, multiple lines of statements can be combined into a block of code that usually begins with an open curly brace ({) and ends with a close curly brace (}). The sample code is as follows
{
console.log('I'm in a block of code.')}Copy the code
This code is not error-free in ECMAScript 5 and earlier, but it is not recommended. This is because the concept and use of block-level scope emerged after ECMAScript 2015.
annotation
Like any other language, the JavaScript language is also a comment, which, like any other language, describes the meaning or function of a piece of code.
Two formats of comments are supported in the JavaScript language:
- One line comment: use
//
Indicates the beginning of a comment. - Multi-line comments: Use
/ *
To start a comment, use* /
As the end of the comment.
The following example code shows the use of comments in both formats of the JavaScript language:
// This is a one-line comment
/* This is a multi-line comment */
Copy the code
Note that multi-line comments cannot be nested, as shown in the following example code:
/* However, you can't, /* nest comments */Syntax error */Copy the code
Keywords and reserved words
A keyword is a set of identifiers that have a special purpose in the JavaScript language and cannot be used as a variable name or function name. Keywords are customized by the ECMA-262 standard and may vary between versions of the ECMA-262 standard.
Reserved words are a set of identifiers that currently have no specific purpose in the JavaScript language and may become keywords in the future.
Keywords and reserved words in JavaScript look like this:
conclusion
Preview: We’ll look at variables in JavaScript in the next article
Excellent articles
01 – what is start from the front 】 【 JavaScript | August more article challenges (juejin. Cn)