156

I missed semicolons in some of the places in my JavaScript, but its not throwing error in any of the browsers. Is the ; at the end needed?

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
kobe
  • 15,671
  • 15
  • 64
  • 91
  • StandardJS doesn’t recommend to use semicolons https://github.com/standard/standard/blob/caa357395b48bc5e1cced3c85a5eb47bbf4aeac8/RULES.md#semicolons – Michael Freidgeim Feb 17 '23 at 23:19

6 Answers6

122

The concept is known as JavaScript Semicolon Insertion or "Automatic Semicolon Insertion". This blog post: JavaScript Semicolon Insertion: Everything you need to know (archived from the original) outlines the concept well in an understandable manner using examples under the headings:

  • Where Semicolons are Allowed
  • Where Semicolons May be Omitted
  • The rules

It even digs into the official ECMAScript specification about the topic.

Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
John K
  • 28,441
  • 31
  • 139
  • 229
  • 5
    As a rule of thumb, line breaks can replace semi-colons with a few intuitive exceptions: (1) empty statements: `while(true);`, (2) inside the for loop syntax `for( ;; )` and (3) when replacing would break the code, e.g.: `a = b \n + c` or `$http(...) \n .success(...)` would be an invalid code if the \n was replaced, so it simply is not. – VinGarcia Sep 15 '17 at 16:11
  • 4
    Ops, I missed 2 rare but important corner cases that will not parse as expected without semi-colons: `a = b \n (function(...){...})()` would parse as if the \n did not exist, and `a = b \n [1,2,3].forEach(...)` also would parse as if it was a single expression. So in these cases, a semi-colon would be necessary. – VinGarcia Sep 15 '17 at 16:37
  • 6
    Link is broken, but it is archived at https://web.archive.org/web/20190308200819/http://inimino.org/~inimino/blog/javascript_semicolons – spacer GIF Jun 19 '19 at 12:41
  • 12
    This is a good example of why links are not answers. Just put the answer into the answer folks! – eric Apr 07 '21 at 18:31
  • 1
    Spec link is invalid – Jackie Jul 09 '21 at 16:46
  • I updated the second link but the first one also appears to be bad – Jackie Jul 09 '21 at 16:49
39

Javascript does something called "semicolon insertion" which means you can actually write code that omits the semicolon in certain places, and they'll basically be added for you when the code is parsed.

The rules around when this happens a little complex. For simplicity's sake, many developers simply pretend semicolon insertion doesn't exist.

ShZ
  • 6,518
  • 1
  • 26
  • 22
  • 96
    Sometimes I fantasize about a world where semicolon insertion doesn't exist. – JAL Oct 23 '10 at 03:11
  • 1
    There was an interesting thread recently on the node.js mailing list that's worth a read: http://groups.google.com/group/nodejs/browse_thread/thread/3166a2bd54e2acf6?pli=1. Check out Isaac Schlueter's coding style; it's an interesting way to take advantage of ASI -- it's not necessarily a bad thing :) – ShZ Oct 23 '10 at 06:22
  • Isaac Schlueter's code style document has moved: https://github.com/isaacs/npm/blob/master/doc/misc/npm-coding-style.md – Pylinux Oct 03 '13 at 08:02
  • 8
    As a python developer, I don't understand why you would do that. Either you use semicolons, in which case your statements can span across as many lines you wish, or you don't, in which case the end of a line means that the statement is over. Now it's like the second one but you always have the thought in the back of your head that there should actually be a semicolon, even if it is completely redundant. – Nearoo Feb 02 '17 at 19:01
26

You can write javascript without semicolon, you only need to insert them if you start a line with a parenthesis ( or a bracket [.

The sugarjs times() function is a good example:

<script>
    var somthing = 1 + 3
    ;(5).times(function(n){
        console.log(n + " line") //prints "X line" 5 times
    })
</script>
codecowboy
  • 9,835
  • 18
  • 79
  • 134
Pylinux
  • 11,278
  • 4
  • 60
  • 67
  • 1
    you also don't need to use html closing tags, browsers will close them automatically – David Fregoli Nov 28 '13 at 11:16
  • 22
    I personally disagree about closing the html tags for two reasons: 1. They are required by the [XHTML](http://codex.wordpress.org/HTML_to_XHTML#All_tags_must_be_closed.2C_even_single_tags) standard. (Semicolons is optional in the [ECMA](http://www.ecma-international.org/ecma-262/5.1/#sec-7.9) standard) 2. They give structure to your code, they says something about where the tags end. (In JavaScript semicolon says the line end, and they are immediately flowed by a newline that says the same) I don't mean to sound harsh, if you and your team writes without closing tags then Godsspeed:-) – Pylinux Nov 29 '13 at 15:21
  • 5
    @DavidFregoli browsers will get that wrong a lot of the time – Ben Taliadoros Dec 21 '16 at 14:33
  • 17
    yeah, automatically closing tags isn't really a feature for the devs, it's more a last attempt of the browser to fix your bad code. – Nearoo Feb 02 '17 at 19:03
  • The linked article contains this statement: "only people can detect and solve software bugs, not tools." Opinions will differ, but that's a large grain of salt for me. – gyratory circus Feb 20 '19 at 21:22
8

To say that writing code with semicolons makes it more readable is absurd. It makes your code more CLUTTERED. Look at the code on this page without the semicolons and tell me it's less readable. It's more readable, less cluttered, cleaner and more elegant. Semicolons are ugly and unnecessary. See this article: https://mislav.net/2010/05/semicolons/

Dave Powers
  • 2,051
  • 2
  • 30
  • 34
Phil Bair
  • 241
  • 2
  • 4
  • 16
    Can't disagree more. I've seen code with few then/else blocks and no semicolons and I cannot trust it ... like a sabre dance on a freshly waxed floor, which used to be reserved as a joke about C language. – Kim Oct 18 '17 at 19:13
  • 4
    I love how the author of the linked article says, "let people and yourself shape your coding style, not some single person or tool", and then ends his article with, "adopt this advice as a rule and you’ll be fine". – DougCouto Oct 10 '19 at 19:24
  • 1
    Not true. Your options with semis are simple. Have vscode auto include them always and not stress. Don't include them and then see a random error once in a while, cos you needed a semi. For me the choice is simple. – powermikee Feb 17 '22 at 23:44
  • 1
    I've used JavaScript for 25 years without semicolons, and never had an issue. It's like people are [afraid of being hit by airplanes while shopping at the grocery store](https://en.wikipedia.org/wiki/Cognitive_bias#List_of_biases). Anyway, your [instinct is correct](https://info.keylimeinteractive.com/less-is-more-minimalism-in-ux-design). When one side of the argument can point to a tangible benefit (time saved by not typing semicolons), but the other side can only point to an extremely rare edge case to support their position, that is an indication of who is correct. – toddmo Nov 28 '22 at 13:57
  • 1
    Swift is a good example of code that doesn't require semicolons, and yet has enough syntax structure to make it easy to read. – clearlight Mar 28 '23 at 03:36
  • I have to strongly disagree with this. In a language designed from the beginning not to use semicolons, it's a blessing to not need them. In a language where you sorta-kinda need them, and the c-style syntax is hacked into not needing them by making assumptions about what you meant and inserting them automatically in ways that could be wrong and break your code, it's a good idea to just use them so your code is predictable. – Anomaly Apr 19 '23 at 14:34
7

Edit 01-20-2020: (There are actually a few obscure cases where not including the semicolon will cause issues in running JS code. Actually what happens is that JS doesn't always know how to intelligently insert semicolons for you. In English the closest thing I can compare this to is the Oxford comma which some people argue you don't need, but we've all seen the jokes about missing punctuation changing the meaning of a sentences. Unless you want to learn all the edge cases or worse find out the hard way then it is recommended to use them always.)

Semicolons are not required for JavaScript programming, nevertheless I advice you to use it. It makes your code more readable and is actually a good practice, and almost all cool programming languages uses it.

Take a stand and use it, it's up to you now!

Urasquirrel
  • 1,461
  • 1
  • 18
  • 34
Necronet
  • 6,704
  • 9
  • 49
  • 89
  • 12
    It's not true that semicolon is not required. From the spec: `Certain ECMAScript statements **must** be terminated with semicolons.` http://bclary.com/2004/11/07/#a-7.9 – John K Oct 23 '10 at 03:14
  • yes you are right i clarify thanks to Automatic Insertion there are many cases you are entitle to avoid it but(a big BUT) as i said before i dont think is good practice to do it – Necronet Oct 23 '10 at 04:05
  • 2
    In Scala and Groovy (and Python) you don't need semicolon, and it is more idiomatic not to use them. What are the "cool" languages you speek of that need semicolon?? – Pylinux May 07 '13 at 14:35
  • All the cool programming languages don't care where you put em, and ignore anything in a line after them. – Darren Ringer Mar 15 '18 at 20:32
  • 1
    Wow, no surprise that this statement still cause controversy, 8 years ago I was a different developer, do not agree with this statement completely but I always keep it as a reminder of how much have I learned!! – Necronet Mar 17 '18 at 07:51
  • 2
    "and almost all cool programming languages uses it." This sentence is a formal fallacy – Filip Bartuzi Apr 26 '18 at 08:56
0

If it's not throwing compiler errors, you should be fine. It's better that you do remember to use them all the time however, as some languages that you might get into such as objective-c are adamant about their use.

Daniel G. Wilson
  • 14,955
  • 2
  • 32
  • 39