Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why was automatic semi-colon insertion (ASI) added to javascript?

Because of a "religious war" at my current work place I have decided to research the history of the ASI feature of javascript.

But I am having trouble finding out why and when ASI was introduced to javascript.

It seems like it has been a feature forever, but is there a specific reason for there to be two ways to terminate a statement in javascript?

Some sources describe ASI as an error-correction feature, which would imply that omitting semi-colons is bad practice.

Is there any performance impacts on relying on ASI?

Personally I prefer semicolons because it makes intentions more explicit, but personal preference isn't a viable argument in any serious discussion.

like image 979
EJTH Avatar asked Jul 08 '15 11:07

EJTH


People also ask

What is automatic semicolon insertion asi?

Automatic Semicolon Insertion (ASI) Unlike other C-like languages, JavaScript does not enforce the use of a semicolon at the end of a statement. Instead, the semicolon is optional, and the JavaScript interpreter will "intelligently" add them when it runs the code.

Why do you have to put a semicolons in JavaScript?

Required Usage: Separate Two Statements on the Same Line If you have two JavaScript statements on the same line, you have to separate them by semicolons. Perhaps the most common example of this is the for loop. The for loop would not work without the semicolons when the looping condition is set up on one line.

Is semicolon mandatory in JavaScript?

Semicolons are not mandatory. They are automatically added at the end of a line if it is missing which actually causes this code to return undefined.


1 Answers

Great question!

Brenden Eich designed the JavaScript programming language originally, and I think it is fair to say that he would agree that automatic semicolon insertion is a design flaw in the language.

We shouldn't blame him. He designed the language in a period of just 10 days in 1995, having no idea that 20 years later it would become (probably) the most important computer language on the planet.

In the following post he says "I wish I had made newlines more significant in JS back in those ten days in May, 1995."

https://brendaneich.com/2012/04/the-infernal-semicolon/

Read on... :)

like image 166
kieranpotts Avatar answered Oct 02 '22 14:10

kieranpotts