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

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)