Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function expression ending with ; vs. not

When reading others code I have seen functions written both ending with a semi-colon and not ending with a semi-colon.

with:

var testFunction = function() {
  // magic happens here
};

without:

var testFunction = function() {
  // magic happens here
}
  • ?: Is one more "technically" correct than the other?
  • ?: Is there a speed advantage to one?
  • ?: Do browsers not care and so it's just a style bleeding over from another language or an old format that used to be required for JavaScript functions?
  • ?: #{question I hadn't considered to ask here but you think I should have}

Update: I also found this very useful => https://developer.mozilla.org/en/JavaScript/Reference/Functions_and_function_scope

like image 513
Mike Grace Avatar asked Dec 08 '10 23:12

Mike Grace


People also ask

Do functions end with semicolon?

Semicolons after function declarations are not necessary. There's no semicolon grammatically required, but might wonder why? Semicolons serve to separate statements from each other, and a FunctionDeclaration is not a statement.

What's the difference between a function expression and function declaration?

The main difference between a function expression and a function declaration is the function name, which can be omitted in function expressions to create anonymous functions. A function expression can be used as an IIFE (Immediately Invoked Function Expression) which runs as soon as it is defined.

What are function expressions?

Function expressions are when you create a function and assign it to a variable. The function is anonymous, which means it doesn't have a name. For example: let myFunction = function() { // do something }; As you can see, the function is assigned to the myFunction variable.

What does => mean in JavaScript?

It's a new feature that introduced in ES6 and is called arrow function. The left part denotes the input of a function and the right part the output of that function.


1 Answers

At it's root, what you have there is an assignment statement, and according to the Google Javascript Style Guide, all statements should end in a ;. Especially assignment statements. JSLint requires ; or else it won't pass.

like image 136
Alex Avatar answered Oct 06 '22 00:10

Alex