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?
6 Answers
The concept is known as JavaScript Semicolon Insertion or "Automatic Semicolon Insertion". This blog post: JavaScript Semicolon Insertion: Everything you need to know 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.
-
4As 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
-
3Ops, 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
-
4Link 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
-
7This 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
-
1Spec 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
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.
- 6,388
- 1
- 24
- 22
-
88Sometimes I fantasize about a world where semicolon insertion doesn't exist. – JAL Oct 23 '10 at 03:11
-
1There 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
-
5As 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
You can write javascript without semicolon, you only need to insert them if you start a line with a parantesis ( 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>
- 10,376
- 4
- 57
- 65
-
1you also don't need to use html closing tags, browsers will close them automatically – David Fregoli Nov 28 '13 at 11:16
-
18I 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
-
13yeah, 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
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/
- 1,666
- 2
- 26
- 29
- 217
- 2
- 3
-
12Can'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
-
3I 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
-
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
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!
- 1,321
- 1
- 17
- 32
- 6,610
- 8
- 47
- 86
-
9It'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
-
2In 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
-
1Wow, 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
-
1"and almost all cool programming languages uses it." This sentence is a formal fallacy – Filip Bartuzi Apr 26 '18 at 08:56
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.
- 14,819
- 2
- 30
- 39
-
10remembering a syntax rule in one language doesn't have any benefit toward other languages. – SingleNegationElimination Oct 23 '10 at 03:02
-
6But the rule of a semicolon is so universal that in general remembering it has many benefits. – Daniel G. Wilson Oct 24 '10 at 01:57
-
3That language-specific rule is by no means universal. Note about half of the 50 languages compared here are NOT semicolon terminated: https://en.wikipedia.org/wiki/Comparison_of_programming_languages_(syntax) – Ezekiel Victor May 07 '19 at 21:31