What is the difference between semicolons in JavaScript and in Python?

JavascriptPythonSyntax

Javascript Problem Overview


Python and JavaScript both allow developers to use or to omit semicolons. However, I've often seen it suggested (in books and blogs) that I should not use semicolons in Python, while I should always use them in JavaScript.

Is there a technical difference between how the languages use semicolons or is this just a cultural difference?

Javascript Solutions


Solution 1 - Javascript

Semicolons in Python are totally optional (unless you want to have multiple statements in a single line, of course). I personally think Python code with semicolons at the end of every statement looks very ugly.

Now in Javascript, if you don't write a semicolon, one is automatically inserted1 at the end of line. And this can cause problems. Consider:

function add(a, b) {
  return
    a + b
}
        

You'd think this returns a + b, but Javascript just outsmarted you and sees this as:

function add() {
  return;
    a + b;
}

Returning undefined instead.

1 See page 27, item 7.9 - Automatic Semicolon Insertion on ECMAScript Language Specification for more details and caveats.

Solution 2 - Javascript

This had me confused for the longest time. I thought it was just a cultural difference, and that everyone complaining about semicolon insertion being the worst feature in the language was an idiot. The oft-repeated example from NullUserException's answer didn't sway me because, disregarding indentation, Python behaves the same as JavaScript in that case.

Then one day, I wrote something vaguely like this:

alert(2)
(x = $("#foo")).detach()

I expected it to be interpreted like this:

alert(2);
(x = $("#foo")).detach();

It was actually interpreted like this:

alert(2)(x = $("#foo")).detach();

I now use semicolons.

JavaScript will only1 treat a newline as a semicolon in these cases:

  • It's a syntax error not to.
  • The newline is between the throw or return keyword and an expression.
  • The newline is between the continue or break keyword and an identifier.
  • The newline is between a variable and a postfix ++ or -- operator.

This leaves cases like this where the behaviour is not what you'd expect. Some people2 have adopted conventions that only use semicolons where necessary. I prefer to follow the standard convention of always using them, now that I know it's not pointless.


1 I've omitted a few minor details, consult ECMA-262 5e Section 7.9 for the exact description.
2 Twitter Bootstrap is one high-profile example.

Solution 3 - Javascript

Aside from the syntactical issues, it is partly cultural. In Python culture any extraneous characters are an anathema, and those that are not white-space or alphanumeric, doubly so.

So things like leading $ signs, semi-colons, and curly braces, are not liked. What you do in your code though, is up to you, but to really understand a language it is not enough just to learn the syntax.

Solution 4 - Javascript

JavaScript is designed to "look like C", so semicolons are part of the culture. Python syntax is different enough to not make programmers feel uncomfortable if the semicolons are "missing".

Solution 5 - Javascript

The answer why you don't see them in Python code is: no one needs them, and the code looks cleaner without them.

Generally speaking, semicolons is just a tradition. Many new languages have just dropped them for good (take Python, Ruby, Scala, Go, Groovy, Io for example). Programmers don't need them, neither do compilers. If a language lets you not type an extra character you never needed, you will want to take advantage of that, won't you?

It's just that JavaScript's attempt to drop them wasn't very successful, and many prefer the convention to always use them, because that makes code less ambiguous.

Solution 6 - Javascript

It is mostly that Python looks nothing like Java, and JavaScript does, which leads people to treat it that way. It is very simple to not get into trouble using semicolons with JavaScript (https://mislav.net/2010/05/semicolons/), anything else is FUD.

Solution 7 - Javascript

Both are dynamic typing to increase the readability. Python Enhancement Proposal 8, or PEP 8, is a style guide for Python code. In 2001, Guido van Rossum, Barry Warsaw, and Nick Coghlan created PEP 8 to help Python programmers write consistent and readable code. Reference

So in Javascript we have the ECMAScript spec that describes how, if a statement is not explicitly terminated with a semicolon, sometimes a semicolon will be automatically inserted by JavaScript engine (called “Automatic Semicolon Insertion” (ASI)). reference

See this article from Google talking about JS too.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionwong2View Question on Stackoverflow
Solution 1 - JavascriptNullUserExceptionView Answer on Stackoverflow
Solution 2 - JavascriptJeremyView Answer on Stackoverflow
Solution 3 - JavascriptcdarkeView Answer on Stackoverflow
Solution 4 - Javascriptuser434817View Answer on Stackoverflow
Solution 5 - JavascripthamstergeneView Answer on Stackoverflow
Solution 6 - JavascriptMatt BriggsView Answer on Stackoverflow
Solution 7 - JavascriptFranz KurtView Answer on Stackoverflow