Why so many semicolons in JavaScript?

2020-06-08 14:36发布

I tend to be a prolific user of semicolons in my JavaScript:

var x = 1;
var y = 2;
if (x==y){do something};

I recently noticed that I was looking at a lot of JavaScript that doesn't have semicolons following if statements. It then occurred to me that I don't even know the preferred syntax for semicolons in JS and after some googling learned (rather surprisingly) that there is no need for semicolons at all aside from splitting statements that are on one line.

So, the question...where did this habit of people using semicolons come from? Is it a remnant from some popular language that was in use at the time JavaScript came into play? Just good practice in general? Is it just me?

I'm probably going to stick with it for no other reason that it's nice when writing jQuery chains to easily spot the end.

UPDATE:

Thanks for all the answers, everyone! It looks like, to summarize things, the reason we see a lot of semicolons in JS even when not needed comes from various variables:

  • older JS minimizer's would produce broken code if you did not insert semicolons
  • many other languages use them, so it's a carried-over habit
  • on occasion, a semicolon CAN change the logic
  • some prefer to use them to make the code more human-readable

10条回答
ら.Afraid
2楼-- · 2020-06-08 15:34

This is probably going to remain a debatable question as to the origin of how this this manner of coding came to be, but it could have come from the ability to differentiate between 1 and many statements within an if conditional, which is common in most languages. And also, people could have confused syntax with JavaScript's object literal

First, you have the single statement if:

if (someNumber == 2)
  doSomething();
else
  doSomethingElse();

Then, you have the multi-statement if (no semi-colon needed):

if (someNumber == 3) {
  doThisFirst();
  doThisSecond();
} else
  doSomethingElse();

This syntax could also be confused with JavaScript's object literal, which does need a semi-color:

var SomeObjectLiteral = {
  property: 2,
  doSomething: function() {
  }
};

Basically, the semi-colon is important in Javascript to determine where a command, or definition, ends. And Braces are just as important to determine blocks of these commands. But, you can have as many semi-colons as you want. If in doubt, use a semi-colon, but be prepared if someone calls you out for not needing one :)

查看更多
乱世女痞
3楼-- · 2020-06-08 15:35

JavaScript requires semicolons. But it does have Automatic Semi-colon Insertion.

The majority of JavaScript programmers are smarter than ASI and prefer to be explicit with their semi colons.

查看更多
Emotional °昔
4楼-- · 2020-06-08 15:40

you can write multiple statement within the function with help of semicolon; semicolon used to separate the statement. easy to understand.

查看更多
你好瞎i
5楼-- · 2020-06-08 15:42

Many computer languages use semicolons to denote the end of a statement. C, C++, and Java are popular examples of this.

As for why people use them despite them being optional, they improve the readability of your code. In most cases it's simply done out of habit, but occasionally you need semicolons in your code to remove possible ambiguity. It's always better safe (and consistent) than sorry.

Here is an example taken from Do you recommend using semicolons after every statement in JavaScript?

// define a function
var fn = function () {
    //...
} // semicolon missing at this line

// then execute some code inside a closure
(function () {
    //...
})();

This will be interpreted as:

var fn = function () {
    //...
}(function () {
    //...
})();

Additionally, semicolons allow Javascript to be packed/minified properly. Otherwise all the statements will be mushed together into one big mess.

查看更多
登录 后发表回答