Home  >  Article  >  Web Front-end  >  Do JavaScript line breaks need to be terminated with a semicolon?

Do JavaScript line breaks need to be terminated with a semicolon?

WBOY
WBOYOriginal
2022-06-15 15:46:042034browse

JavaScript line breaks do not have to end with a semicolon; if each statement is written as a separate line, there is no need to end it with a semicolon, but if the next line encounters "(", "[", "/ ", " ", or "-", JavaScript may be combined with the next line for interpretation, so you need to use a semicolon to separate the two statements.

Do JavaScript line breaks need to be terminated with a semicolon?

The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.

Does JavaScript line break need to end with a semicolon?

About whether to add the last line of each code The semicolon problem

has such a characteristic: if a statement starts with "(", "[", "/", " ", or "-", then it is very likely to be combined with the previous statement. Explain together.

In other words, when writing javascript, if each statement is written on a separate line, there is no need to write a semicolon, but if the next line encounters the symbols mentioned above, javascript The explanation may be combined with the next line. In this case, a semicolon is needed to indicate that these are two statements.

See the following case for details:

Start with "("

a = b
(function(){
})()
//=============================================
//此时js会把上面的语句解释成:
a = b(function() {
})();

Starts with "["

a = function() {
 
}
[1,2,3].forEach(function(item) {
 
});
//=============================================
//此时js会把上面的语句解释成:
a = function() {
}[1,2,3].forEach(function(item) {
 
});

Starts with "/"

a = "abc"
/[a-z]/test(a)
//=============================================
//此时js会把上面的语句解释成:
a = "abc"/[a-z].text(a);

Starts with " " or "-"

a = b
 + c
//=============================================
 //js会把上面的语句解释成:
a = b + c;
a = b
- c
//=============================================
//此时js会把上面的语句解释成:
a = b - c;

In addition, if in return, break , continue, throw and other keywords, JavaScript will fill in the semicolon at the new line.

For example:

return 
{
a : 1
}
//=================================
//此时js会把上面的语句解释成:
return;
{
a : 1
}

If self-increment, self-decrement – ​​is used as the suffix of an expression, the expression It is best to write them on the same line, otherwise an error will be reported

as follows:

x
++
y
//上面的语句并不会被解释成
x++;
y;
//而会被解释成
1
2
x;
++y;

[Related recommendations: javascript video tutorial, web front-end]

The above is the detailed content of Do JavaScript line breaks need to be terminated with a semicolon?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn