How do I prevent JavaScript from automatically inserting semicolons

  • 2020-09-27 00:59:40
  • OfStack

In JavaScript, the semicolon at the end of a line has an automatic insertion mechanism, which allows some friends to ignore the input semicolon. Of course, you'd better get in the habit of typing semicolons, and learn how JavaScript handles ignoring semicolons, as this knowledge will help you understand code without semicolons.

The JavaScript language has a mechanism for automatically inserting a semicolon after a sentence to modify a semicolon delimiter that is missing at the end of a statement during parsing. However, because this automatically inserted semicolon conflicts with another mechanism of the JavaScript language, where all whitespace characters are ignored, programs can format code with whitespace.

The conflict between these two mechanisms can easily mask more serious parsing errors. Sometimes semicolons are inserted inappropriately. For example, automatically inserting a semicolon into an return statement will result in the following: If the return statement is to return a value, the expression for this value must begin on the same line as return, for example:


 var f = function (a) {
 return
 {
 status: true
 };
 }

It looks like we're returning an object that contains the status member element. Unfortunately, JavaScript automatically inserts a semicolon to return undefined, causing the object that is actually returned below to be ignored.

There are no warnings when automatically inserting a semicolon causes a program to be misunderstood. This problem can be avoided by placing {at the end of the first row instead of the head of the next row, for example:


 var f = function (a) {
 return {
 status: true
 };
 }

To avoid errors caused by the omission of semicolons, it is recommended to develop a good habit of adding a semicolon to complete sentences, regardless of whether they are complete or not.

For ease of reading, when long sentences need to be displayed in branches, make sure that no complete logical semantics can be formed in line 1. For example, the following code is a continuous assignment statement that can be displayed in a line to see their relationship more clearly. This branch shows that JavaScript does not treat each line as a separate sentence, and thus does not generate ambiguity, since no independent logical semantics can be formed within a line.


 var a =
 b =
 c = 4;

The above statement is shown in line 1 as follows: var a = b = c = 4;

For the following statement, it is easy to generate ambiguity if it is not displayed in the correct line. Define a variable i, then assign it a value of 1 if a is true, otherwise determine the variable b, 2 if b is true, otherwise determine the variable c, 3 if c is true, otherwise assign it a value of 4.


 var i = a ? 1 : b ? 2 : c ? 3 : 4;

The following line display is incorrect because of the expression a ? 1: b can form independent logical semantics, so JavaScript will automatically add a semicolon after it to represent a separate sentence.


 var i = a ? 1: b
 ? 2 : c
 ? 3 : 4;

The safe approach would be to use the following line display so that each line does not form its own semantics.


 var i = a ? 1
 : b ? 2
 : c ? 3
 : 4;

In conclusion, when writing code, make a good habit of ending sentences with semicolons, which should be used to separate complete sentences. A sentence displayed in a line should ensure that a single line does not easily form independent logical semantics

PS: Example details javascript automatically adds a semicolon at the end of a line

The semicolon (;) , usually used at the end of line 1, as follows:


var webName = " The home of the script ";
var url = www.ofstack.com;

The above code, which adds a semicolon after each declaration, needs little explanation.


var webName = " The home of the script "
var url = www.ofstack.com

The semicolon can also be omitted, but the semicolon omitted is automatically added at compile time.
Let's look at another code example:


function done(webName){
 return
 webName
}
console.log(done(" The home of the script "));

Since the compiler automatically adds a semicolon after each line, the output value undefined is not "this site."


Related articles: