Languages
[Edit]
EN

JavaScript - when to put semicolon after function definition

3 points
Created by:
Gaia-Kirby
810

In this article, we would like to show you when to put a semicolon after a function in JavaScript.

Rule:

Semicolons are used to separate statements.

 

Hints:

1. Semicolons aren't necessary after function declarations (since they are not statements).

function myFunction() {
    // ...
}

 

2. It is recommended to use semicolons after:

a) function expressions, because not using them may lead to bugs.

Old JSES6+
var myFunction = function() {
    // ...
}; /* <------- Semicolon is recommended */
const myFunction = () => {
    // ...
}; /* <------- Semicolon is recommended */
(function() {
    // ...
})(); /* <---- Semicolon is recommended */
(() => {
    // ...
})(); /* <---- Semicolon is recommended */

b) functions created with new operator, because not using them may lead to bugs.

var myFunction = new Function('/* ... */'); /* <---- Semicolon is recommended */

 

References

  1. Semicolon - Wikipedia

Alternative titles

  1. JavaScript - when we should put semicolon after function definition?
  2. JavaScript - when to put semicolon after function declaration
  3. JavaScript - when to put semicolon after function statement
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

â€ïžđŸ’» 🙂

Join