Learn JavaScript basics every day (2)—Do you add or not add semicolons in JavaScript?

Learn JavaScript basics every day (2)—Do you add or not add semicolons in JavaScript?

Do you add or not add semicolons in JavaScript?

1 Introduction

Most programming languages we touch will to a semicolon as the end code, the program statement separates, JavaScript the same. This is very important to enhance the cleanliness and readability of the code. Without a separator, the end of one sentence becomes the beginning of another sentence, and vice versa. When we first learned programming and contacted the C language, we often caused program bugs because there was no delimiter at the end of the code or the delimiter was written incorrectly.

For many programming languages, it is necessary to add a semicolon at the end of a statement. However, for JavaScript, this is not the case. In JavaScript, if the statements are on their own line, you can usually omit the semicolon between the statements (the semicolon at the end of the program or before the right "}" can also be omitted.)

Therefore, when writing JavaScript programs generally have two coding habits, add a semicolon and not a semicolon :

  • Add a semicolon: Many programmers use a semicolon to clearly mark the end of a statement, even when the semicolon is not completely needed;
  • No semicolon: Another style is to omit semicolons wherever they can be omitted, and use semicolons only when they have to.

Let's record and explain the details of the use of semicolons in JavaScript.

2. Under what circumstances can you not add a semicolon

Without adding a semicolon, JavaScript will automatically insert a semicolon when parsing. When the following two conditions are met, JavaScript will automatically insert a semicolon , and it is not necessary to add a semicolon:

2.1 Each statement occupies a line

If each statement in multiple statements occupies one line, the semicolon at the end of the statement can be omitted, and JavaScript will automatically fill in the semicolon when parsing, for example:

let x = 10
let y = 20
console.log(x+y)//3

If written on the same line, you must add a semicolon:

let x, y
x = 10 y = 30  
console.log(x+y)//SyntaxError: Unexpected identifier

Do not write a semicolon to report an error.

2.2 Without a semicolon, the upper and lower lines of code cannot be parsed

JavaScript does not fill in semicolons at all line breaks, and only fills in semicolons when the code cannot be parsed correctly without a semicolon;

The following example:

let a 
a
=
3
console.log(a)//3

Obviously, the code is parsed as:

let a;
a=3;
console.log(a);

No semicolons are inserted in 2 or 3 rows.

note:

The separation rules of these statements will lead to some unexpected situations, such as the following example;

let m = n + f
(b+c).toString()

But the statement will eventually be parsed as:

let m = n + f(a+b).toString();

This is inconsistent with the meaning expressed by the source code, but there is no error when it is parsed.

3. Special circumstances

In layman's terms, if a sentence starts with "(", "[", "/", "+", "-", they are most likely to be parsed together with the previous sentence.

If the previous statement cannot be combined and parsed with the next statement, JavaScript will insert a semicolon after the first statement. This is a general rule.

However, there are 2 exceptions,

3.1 return, break and continue

If return, break, continue are involved, if these three keywords are followed by a newline, a semicolon must be inserted after the keyword;

Such as:

return
true

Will be parsed into

return;
true;

This obviously violates the original intent of the code.

3.2 When there is "++" or "--" operator

If the "++" and "--" operators are involved, these expressions can be used as the prefix or suffix of the expression.

If it is used as the suffix of the expression, it should be on the same line as the expression. Otherwise, JavaScript will add a semicolon at the end of the line, and "++" "--" will be used as the prefix operator of the next sentence and the following Parse one sentence together.

Such as:

x
++
y

This code parses as:

x;
++y;

Instead of:

x++;y;

4. Benefits of using semicolons

  • Adding a semicolon can avoid many errors (for example, incomplete input), and developers can safely compress the code by deleting extra spaces;
  • Adding a semicolon can improve the performance of the code in some cases, because the parser does not have to spend extra time figuring out where to add the semicolon.

5. References

  • "JavaScript Advanced Programming"
  • "JavaScript Definitive Guide"
Reference: https://cloud.tencent.com/developer/article/1677973 Learn JavaScript basics every day (2)-The semicolon in JavaScript, do you add it or not? -Cloud + Community-Tencent Cloud