Semicolon before (()=>true)()
It works fine:
const foo = 1; // any number, string, bolean or object (() => console.log('stuff'))()
But it doesn't work without semicolon:
const foo = 1 // TypeError: 1 is not a function (() => console.log('stuff'))()
Hm... Should not the call of an anonymous function be treated as a separate instruction in the case when the first bracket can not be interpreted as a correct continuation of the previous instruction?
Yes, but it's only about syntactically correct continuations.
1(() => console.log('stuff'))()
is a syntactically correct expression and parses as "call
1 with an argument of
() => console.log('stuff'), then call the result of that without arguments". This throws an exception at runtime (
1 is not function, so it can't be called), but it's still a valid expression.
Semicolons: A Quick Guide How to Use a Semicolon, You can use a semicolon to join two closely related independent clauses. Let's put that another way. The group of words that comes before the semicolon should You should use a semicolon before a conjunctive adverb, not after. Final Thoughts: How and When to Use a Semicolon. A semicolon is a unique type of punctuation mark used to join independent clauses or divide items in a list. In many instances semicolons can be used interchangeably with periods; however, they have their own rules that govern
In your case, it is interpreting that you are calling a function.
A good article on the topic on how semicolons are automatically inserted:
The norm: The parser treats every new token as part of the current statement, unless there is a semicolon that terminates it. The following examples show code where you might think a semicolon should be inserted, but isn’t. This illustrates the risks of omitting semicolons.
No ASI:a = b + c (d + e).print()
This does not trigger ASI, because the opening parenthesis could follow c in a function call. The above is thus interpreted as:a = b + c(d + e).print();
Using semicolons before conjunctions (and, or, but, etc.), When a sentence made up of two independent clauses contains commas, it is possible to use a semicolon before a conjunction which joins the two independent A semicolon isn’t the only thing that can link two independent clauses. Conjunctions (that’s your ands, buts, and ors) can do that too. But you shouldn’t use a semicolon and a conjunction. That means when you use a semicolon, you use it instead of the ands, buts, and ors; you don’t need both.
…when the first bracket can not be interpreted as a correct continuation of the previous instruction?
But it can - as you can see, the code parses just fine, and executes. That it will throw a runtime exception when no semicolon isn't inserted doesn't matter to the ASI, it cannot know while parsing.
'However' (Period (Full Stop), Comma, or Semicolon Before?), Do not use a comma before 'however' (or any other conjunctive adverb) when it serves as a bridge between two sentences (or independent clauses). You can Semicolons and commas are used to link two sentences or independent clauses. An independent clause must contain a subject and a verb. You have the choice of leaving one independent clause alone and ending it with a period, or you may link two independent clauses together with either a comma or semicolon.
in your second code snippet, it just actually equals:
const foo = 1 (() => console.log('stuff'))()
which means you invoke a function called '1' and pass '()=>console.log('stuff')' as an argument. but apparently 1 is not a function, so it throw an error, hope make sense to you
Use a semicolon before introductory words or connectors, THE SEMICOLON. Use a semicolon to join/separate closely related, complete sentences. The semicolon is stronger than a comma, but weaker than a period Use a Period (Full Stop) or a Semicolon before "However" A transitional phrase like "however" will usually start a new sentence, but if you would like a smoother transition than that afforded by a period, you can use a semicolon before it to merge the new sentence with the previous one. Here are some examples (transitional phrases shaded):
A Different Meaning for the Period or Semicolon Before "Is That , Deciding to use a period versus a semicolon before “Is that correct?” and expecting your reader to distinguish that they mean something A semicolon is also often used before introductory expressions such as for example, that is, and namely, in place of a colon, comma, dash, or parenthesis: On one important point Harry and Mabel agreed; that is, it would be better for all if Harry found somewhere else to be while Mabel finished cooking.
The Semicolon, Generally, you should not place a semicolon before a coordinating conjunction that links two independent clauses. The only exception to this guideline is if the 2.) Before a conjunction. When a sentence contains multiple commas in the first clause, it is appropriate to use a semicolon before a conjunction to join two clauses. When too many commas are used in one sentence, the sentence becomes confusing. A semicolon helps to break up the clauses and avoid confusion for the reader. Examples:
Semicolons, colons, and dashes – The Writing Center, Semicolons. The semicolon looks like a comma with a period above it, and this can be a good way to remember what it does. A semicolon creates The Uses of the Semicolon. The semicolon (;) is a punctuation mark in English separating elements but used much less than the comma. It is more often used in more advanced extended sentences and adds a formal tone. Its name implies that it indicates a separation that is neither full (as indicated by the period), nor minute (as indicated by a comma).