Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 839
wong2 Avatar asked Aug 28 '11 07:08

wong2


People also ask

Is it better to use semicolons in JavaScript?

Semicolons are an essential part of JavaScript code. They are read and used by the compiler to distinguish between separate statements so that statements do not leak into other parts of the code. The good news is that JavaScript includes an automatic semicolon feature.

Can semicolons be used in Python?

A semi-colon in Python denotes separation, rather than termination. It allows you to write multiple statements on the same line. This syntax also makes it legal to put a semicolon at the end of a single statement: print('Why God?


2 Answers

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.

like image 126
NullUserException Avatar answered Sep 20 '22 10:09

NullUserException


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.
2Twitter Bootstrap is one high-profile example.

like image 39
Jeremy Avatar answered Sep 17 '22 10:09

Jeremy