Do we need semicolon at the end?

Javascript

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

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.

Solution 2 - Javascript

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.

Solution 3 - Javascript

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>

Solution 4 - Javascript

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/

Solution 5 - Javascript

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!

Solution 6 - Javascript

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkobeView Question on Stackoverflow
Solution 1 - JavascriptJohn KView Answer on Stackoverflow
Solution 2 - JavascriptShZView Answer on Stackoverflow
Solution 3 - JavascriptPylinuxView Answer on Stackoverflow
Solution 4 - JavascriptPhil BairView Answer on Stackoverflow
Solution 5 - JavascriptNecronetView Answer on Stackoverflow
Solution 6 - JavascriptDaniel G. WilsonView Answer on Stackoverflow