Files
freeCodeCamp/guide/english/javascript/semicolons/index.md
Randell Dawson 0a1eeea424 fix(guide) Replace invalid prism code block names (#35961)
* fix: replace sh with shell

fix replace terminal with shell

fix replace node with js

fix replace output with shell

fix replace cs with csharp

fix replace c++ with cpp

fix replace c# with csharp

fix replace javasctipt with js

fix replace syntax  with js

fix replace unix with shell

fix replace linux with shell

fix replace java 8 with java

fix replace swift4 with swift

fix replace react.js with jsx

fix replace javascriot with js

fix replace javacsript with js

fix replace c++ -  with cpp

fix: corrected various typos

fix: replace Algorithm with nothing

fix: replace xaml with xml

fix: replace solidity with nothing

fix: replace c++ with cpp

fix: replace txt with shell

fix: replace code with json and css

fix: replace console with shell
2019-05-15 19:08:19 +02:00

1.9 KiB

title
title
Semicolons

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 always active by default and 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 for more information on how ASI works.

There is only one case where ASI fails: when a line starts with an opening bracket (. 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:

;(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.

Errors you might run into

When JavaScript was first made it was meant to aid beginners to get into programming. Nobody wants to be searching for a dang semi-colon in their code when they first start programming. So the choice of semi-colons was implemented, as stated above they are technically there.

For example:

function foo(x) {
  return
    function(y) {
      return x + y;
    }
} 
let z = foo(10);
z(10)// TypeError z is not a function
// Because of Automatic Semicolon Insertion, our inner function does not exist.

JavaScript will implement semi-colons where they are expected.

Other resources

An Open Letter to JavaScript Leaders Regarding Semicolons