I hope I won't get downvotes in this post.
But I can't figure out why Javascript needs you to insert semicolon at the end.
Can't it figure out when the line ends?
And will Javascript ever get rid of semicolon you think?
I hope I won't get downvotes in this post.
But I can't figure out why Javascript needs you to insert semicolon at the end.
Can't it figure out when the line ends?
And will Javascript ever get rid of semicolon you think?
Take a look at minified JavaScript and then try and imagine it without the semicolon; being able to throw out the whitespace can have significant enough impacts on file-sizes and that can be reason enough.
Less specifically it's very much a give-and-take. Firstly, in programming there is a certain necessary amount of punctuation. Try reading a book and ignoring the periods and capital letters, and see how much the meaning can become ambiguous as the sentences fall apart. The same is true in programming; without the punctuation you can no longer clearly interpret what is being said.
The give and take comes in terms of how you represent those end-of-statements. Some languages such as Python use stricter white-space conventions to denote where statements begin and end, leading to code which is very easy to read. Other languages are much more strict than JS in terms of syntax requirements--which can become quite hard to read, but is also very explicit.
Personally, I quite like having line-end statements. It helps me to read the code and, if I'm feeling quirky, lets me put more than one statement on the same line. It also allows for compression, and avoids the quirks presented by different OSes using different end-of-line/new-line characters.
So, in short, I really hope not (unless it's to replace it with another end-of-line/end-of-statement character, but that would just be pointless).
Personally, languages that don't need an EOL character annoy me.
Formatting my code, let's say, a SQL statement is so much better:
$SQL = "SELECT
*
FROM
table";
Vs
SQL = "SELECT * FROM table"
Sure, it doesn't matter much for this SQL statement, but when you start getting complex ones...
SQL = "SELECT customers.* FROM customers LEFT JOIN orders ON (customers.customer_id = orders.customer_id AND year(orders.order_date) = 2004) WHERE orders.order_id IS NULL"
$SQL = "SELECT
customers.*
FROM
customers
LEFT JOIN
orders
ON (
customers.customer_id = orders.customer_id
AND year(orders.order_date) = 2004
)
WHERE
orders.order_id IS NULL
Which is more readable to you?
Semicolon is a formally necessary part of java script statements:
Practically speaking, it is not needed in most cases. The browser can usually parse statements based on new-lines, line-breaks and other informal conventions.
It is a good idea to put it anyways, though, because for example, a person may have the option to use strict JS in Mozilla turned on, and they might get syntax warnings in the console without it.
Also, in certain cases (e.g., in a bookmark let) the lack of the semicolon may cause the evaluation of the statement to fail because there is no new-line or space or line-break between two statements (e.g., var a="Henry the ",b=7;++b;alert(a+b+"th\nI am, I am...");).
Also, Javascript takes its syntax from C. It wouldn't make sense to just take away semi colons for JS.
It's like the period at the end of a sentence. You don't need it per se, but it increases readability and allows multiple statements to (judiciously) be placed on one line.
It also increases the possibilities for minification by providing a concrete separator.
It is a deterministic statement separator; there is no doubt about its meaning or use.
Lastly, removing the requirement for a semicolon would also make Javascript inconsistent with most other C-style languages which--like it or not--comprise a huge codebase that won't be changing any time soon.
The ; is not a EOL convention. It depicts the end of a process request.
DO THIS; DO THAT; DO MORE;
By convention people have become accustomed to placing it on a single line, but it's completely unnecessary. You can have a single process request span multiple lines or multiple processes on a single line. The only way to know when it ends is by the ; declaration.
Therefore, it is ABSOLUTELY necessary to declare the end of the process. In Javascript you can get away with not using the ; (in some cases) when it is immediately followed by other process closures such as the brace (}).
It also stems from older languages such a C. So your question should be why do programming languages use characters such as ; to declare the end of a process? (see my answer above and others as well)
You need it because of IE. You can end up with bizzarre and subtle bugs without it, and the rules for when you actually need it or not are rather arcane, so it is a best practice to use it all the time.
That being said, adding a keystroke at the end of every line for no real reason is the very essence of useless noise in code, I would recommend checking out the excellent coffeescript, which eliminates most of the silly things from javascript syntax.
Actually, JavaScript does mostly work if you leave out all the semicolons. The browser has to try to figure out where your statements end, which it does according to a standard set of rules. It's called automatic semicolon insertion.
However, it's a terrible idea to rely on this. The rules are complicated and they might not always do what you expect. For example, what does this program mean?
f(x)
(new Foo).start()
It turns out that JS does not treat this as two separate statements. This is the same as:
f(x)(new Foo).start();
which... is probably total nonsense, but actually there's a JS library, (fab), that depends on JS treating this as a single statement.
A major problem with leaving off the semicolons is that if you make a mistake, sometimes JavaScript accepts your program, but interprets it differently from the way you intended.
(Also: every time a new bit of syntax is proposed for the ECMAScript standard, the committee has to worry about whether it changes the meaning of existing JS code with missing semicolons! It's a drag.)
To answer your original question, "Will Javascript ever get rid of semicolon at the end?": no. That would, of course, make your browser reject every existing scrap of JS code on the Web. The existing rules are very well established and necessary for compatibility with the Web as it is.
In most cases, semicolons are unnecessary in Javascript. The interpreter can usually makes sense of things without them. But there are times when they are needed to resolve ambiguity. For this reason, the Google style guide for Javascript (for example) recommends always using them.
Because of these ambiguities, it's doubtful that Javascript will ever get rid of semicolons. The syntax would have to change significantly and too much old code would break.