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?
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:
You’d think this returns
a + b, but Javascript just outsmarted you and sees this as:Returning
undefinedinstead.1 See page 27, item 7.9 – Automatic Semicolon Insertion on ECMAScript Language Specification for more details and caveats.