Why should we use a semicolon after every function in JavaScript?

JavascriptWeb DevelopmentFront End Technology

Adding semicolons after every function is optional. To avoid undesirable results, while using functions expressions, use a semicolon.

Using semicolon

var display = function () {
  alert(“Hello World”);
};
(function () {
  // code
})();

Without using semicolon

var display = function () {
  alert(“Hello World”);
}
(function () {
  // code
})();

The first function executes immediately since we have used the semicolon.

raja
Published on 30-Jan-2018 12:01:43
Advertisements