EN
JavaScript - when to put semicolon after function definition
3 points
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.
1. Semicolons aren't necessary after function declarations (since they are not statements).
xxxxxxxxxx
1
function myFunction() {
2
// ...
3
}
2. It is recommended to use semicolons after:
a) function expressions, because not using them may lead to bugs.
Old JS | ES6+ |
xxxxxxxxxx 1 var myFunction = function() { 2 // ... 3 }; /* <------- Semicolon is recommended */ |
xxxxxxxxxx 1 const myFunction = () => { 2 // ... 3 }; /* <------- Semicolon is recommended */ |
xxxxxxxxxx 1 (function() { 2 // ... 3 })(); /* <---- Semicolon is recommended */ |
xxxxxxxxxx 1 (() => { 2 // ... 3 })(); /* <---- Semicolon is recommended */ |
b) functions created with new
operator, because not using them may lead to bugs.
xxxxxxxxxx
1
var myFunction = new Function('/* ... */'); /* <---- Semicolon is recommended */