I need a break so I thought I'd post a thought about the semicolon.
Throughout much of its history the languages of computer science have required semicolons at the end of statements. This was a great help syntactically, making it easy for the compiler/interpreter to recognize the end of statements.
Not all languages required semicolons, but most. Assembly languages didn't use the semicolon because it was one line per instruction, no statement separator needed. Fortran was also only one statement per line (per IBM card in the earlier days), so no semicolon. But almost all the higher level languages that came after required semicolons between statements, COBOL being a notable exception. Languages I've used that required semicolons were Bliss Bliss32, C, C++, Perl and PHP.
Language requiring no semicolons that are increasing in popularity are Python and Swift. Semicolons are only necessary if you're including multiple statements on the same line.
And then there's Javascript. In Javascript semicolons are optional. Mostly. These statements need no semicolon:
let arr = []
arr[0] = 5
arr[2] = 10
arr[3] = 25
arr.join(', ') // Value is "5, 10, 25" due to implicit conversion of the integers to strings
But this will not work because the last line is illegal syntax:
let arr = []
arr[0] = 5
arr[2] = 10
arr[3] = 25
(arr[0] + arr[1] + arr[2]).toString() // Illegal syntax
The reason that it's illegal syntax is that the "25" is followed immediately by an open paren on the next line, which causes the interpreter to interpret the 25 as a function and the open paren as the beginning of its argument list. So we have to add a semicolon after the "25":
let arr = []
arr[0] = 5
arr[2] = 10
arr[3] = 25;
(arr[0] + arr[1] + arr[2]).toString() // Legal syntax, value of the string is "40"
I've been writing Javascript since the early 2000's and have always used semicolons after every statement. Normally I would include a semicolon after every line, like this:
let arr = [];
arr[0] = 5;
arr[2] = 10;
arr[3] = 25;
(arr[0] + arr[1] + arr[2]).toString(); // Legal syntax, value of the string is "40"
That was my habit. Not using semicolons seemed blasphemous, plus I was writing copious amounts of code in Perl and later in PHP, both of which require semicolons. Switching back and forth between using and not using semicolons would have caused numerous errors of omission.
But recently I've been working on a website that is Javascript only, and I've been leaving out the semicolons. The cases where a semicolon is required, like the example above, almost never arise is real code, especially in the NodeJS/ReactJS context I'm currently working in. Problems do arise when I make changes to the PHP code of this website where semicolons are required and which now I keep forgetting.
But I plan to write increasing amounts in code in Javascript, so I'll continue leaving out the semicolons.
--Percy