简体   繁体   中英

At end of JS function declaration, adding semicolon, why jshint prompt an error

When I declared a js function, and added a semicolon at the end, which is: function foo(){};

Then jshint prompted unnecessary semicolon . Why?

A function (or class) declaration is a complete statement; it should not have a semicolon.

You only need a semicolon for regular executable statements (including const x = function() { ... }; ).

there are 2 types of function writting

  • function declaration (; not allowed)
  • function expression (; allowed)

see the below example

function _functionName() {

};  // this is not required



var _functionName = function() {

}; //this is okey

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM