# Should you use semicolons in Javascript?

**URL:** <https://forum.freecodecamp.org/t/should-you-use-semicolons-in-javascript/80595>\
**Category:** freeCodeCamp Contributors\
**Tags:** guide\
**Created:** [January 25, 2017, 9:40pm UTC](https://forum.freecodecamp.org/t/should-you-use-semicolons-in-javascript/80595 "2017-01-25T21:40:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![camperbot](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/camperbot/32/18364_2.png) [@camperbot](https://forum.freecodecamp.org/u/camperbot)\
**Post date:** [January 25, 2017, 9:40pm UTC](https://forum.freecodecamp.org/t/should-you-use-semicolons-in-javascript/80595/1 "2017-01-25T21:40:05Z")

</div>

Semicolons are not required in Javascript. This is because Javascript has a feature called “Automatic Semicolon Insertion,” or ASI for short. ASI puts semicolons in your Javascript for you. It is active by default and always active, it’s a part of the language and can not be disabled.

ASI has a set of rules it uses to determine where it should insert semicolons. If there is already a semicolon in place, it won’t change anything. See [this StackOverflow answer](http://stackoverflow.com/a/2846298/3467946) for more information on how ASI works.

There is only one case where ASI fails: when a line starts with an opening bracket `(` or `[`. To avoid this causing errors, when a line starts with an opening bracket, you can put a semicolon at the beginning of the line that has the opening bracket:

```js
;(function() {
  console.log('Hi!')
})

```

Note that this is only required if you don’t use semicolons.

A consistent coding style makes code more readable. Decide whether you will or won’t use semicolons, and do so everywhere.

### Other resources

[![](https://i.ytimg.com/vi/Qlr-FGbhKaI/hqdefault.jpg "Semicolons cannot save you! - FunFunFunction #9") ](https://www.youtube.com/watch?v=Qlr-FGbhKaI)

> **[An Open Letter to JavaScript Leaders Regarding Semicolons](https://blog.izs.me/2010/12/an-open-letter-to-javascript-leaders-regarding/)**
>
> Writing and Stuff from Isaac Z. Schlueter

---

<div class="post-metadata">

**Author:** ![rwebaz](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/rwebaz/32/36301_2.png) [@rwebaz](https://forum.freecodecamp.org/u/rwebaz)\
**Post date:** [January 25, 2017, 10:31pm UTC](https://forum.freecodecamp.org/t/should-you-use-semicolons-in-javascript/80595/2 "2017-01-25T22:31:16Z")

</div>

Thx, [camperbot](https://forum.freecodecamp.com/users/camperbot/activity). Mozilla’s [Old school](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide) sez use semi-colons … but, if you do … will this create a dupe, or simply an override by the system you’ve described?

---

<div class="post-metadata">

**Author:** ![adityaparab](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/adityaparab/32/64616_2.png) [@adityaparab](https://forum.freecodecamp.org/u/adityaparab)\
**Post date:** [January 26, 2017, 12:57pm UTC](https://forum.freecodecamp.org/t/should-you-use-semicolons-in-javascript/80595/3 "2017-01-26T12:57:40Z")

</div>

Taking Camperbot’s popularity in consideration, I’d like take on a daring adventure of pointing out a few gotchas when it comes the topic `semicolons are optional in JS` and show that how it’s a false assumption. And why explicitly using semicolons is a good practice in general.

Javascript is a language for non complainers. Unlike Java, for example.  
No semicolon in JS? No problem!  
No semicolon in Java? Government shutdown, reelection!

In case semicolon is not present at the end of an `expression`, js engine will add them for you.

So semicolons in JS are optional, or are they?

Consider this example

```auto
function foo(){
  var id="foo"
  return
    id
}

function bar(){
  var id='bar'
  return id
}

console.log(foo()) //undefined
console.log(bar()) // bar

```

So why dud you get `undefined` instead of `foo` ?

Well that’s the thing with non complaining JS interpreter. It adds a semicolon at the end of an expression.  
here the things that succeed `return` are optional. So the interpreter will assume this as a complete expression and put a semicolon. So the code generated by interpreter will look like this

```auto
function foo(){
  var id="foo";
  return;
    id;
}

```

Notice the added semicolons.

Now this is the case where the similar code structure would work inspite of not having semicolons

```auto
function foo(){
  var id="foo"
  return 5 +
    id
}

console.log(foo()) //5foo

```

Now in this case the lack of semicolons didn’t confuse your interpreter for `5 +` is not a complete expression. So the interpreter assumes the presence of 2 operand for `+` somewhere in the remaining part of the code.

So, in a nutshell. The semicolons are NOT options in JS. The interpreter adds semicolons for you.  
So if you choose to skip semicolons while writing code, it is your responsibility to resolve any such confusing expressions beforehand.

---

<div class="post-metadata">

**Author:** ![systimotic](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/systimotic/32/33790_2.png) [@systimotic](https://forum.freecodecamp.org/u/systimotic)\
**Post date:** [March 2, 2017, 7:40pm UTC](https://forum.freecodecamp.org/t/should-you-use-semicolons-in-javascript/80595/4 "2017-03-02T19:40:31Z")

</div>

@rwebaz ASI does not touch change anything if there is already a semicolon in place. I’ve updated the post to clarify this.

@adityaparab I recommend checking out the resources linked in the original post. They both mention the example case you bring up. The important thing to note here is that if you add semicolons yourself, the exact same problem will occur.

---

<div class="post-metadata">

**Author:** ![anon52159105](https://avatars.discourse-cdn.com/v4/letter/a/45deac/32.png) [@anon52159105](https://forum.freecodecamp.org/u/anon52159105)\
**Post date:** [August 6, 2017, 11:25pm UTC](https://forum.freecodecamp.org/t/should-you-use-semicolons-in-javascript/80595/5 "2017-08-06T23:25:40Z")

</div>

Closed on August 6, 2017, 11:25pm UTC


