2

MDN uses the second code I have provided and it runs fine but throws an error at the end. Why did they end the anonymous function with a semicolon? Is it ok to have an anonymous function if its not going to be in a function expression? Functions aren't supposed to end in semicolons if they aren't function expressions.

function makeAdder(x) {
  return function(y) {
    return x + y;
  }
}

var add5 = makeAdder(5);
var add10 = makeAdder(10);

console.log(add5(2));  // 7
console.log(add10(2)); // 12

versus

function makeAdder(x) {
  return function(y) {
    return x + y;
  };
}

var add5 = makeAdder(5);
var add10 = makeAdder(10);

console.log(add5(2));  // 7
console.log(add10(2)); // 12
Ivar
  • 6,138
  • 12
  • 49
  • 61
  • 1
    How about `makeAdder = x => y => x + y`? ;) – georg Mar 27 '19 at 08:12
  • 1
    "*Functions arent supposed to end in semicolons if they arent function expressions.*" but what makes you think this isn't a function expression? – VLAZ Mar 27 '19 at 08:14
  • Related (maybe even duplicate): [Why should I use a semicolon after every function in javascript?](https://stackoverflow.com/questions/1834642/why-should-i-use-a-semicolon-after-every-function-in-javascript) – Ivar Mar 27 '19 at 08:28
  • @george i can recognize a arrow function when i see one but i dont know how to write them ill learn how to write them after i master scopes and closures – HeavensGate666 Mar 27 '19 at 08:39
  • @VLAZ its not stored in a variable so i dont think its a function expression – HeavensGate666 Mar 27 '19 at 08:39
  • @HeavensGate666 it doesn't have to be assigned to a variable to be a function expression. It's simply an expression that *returns* a function. You can then assign that returned value to a variable, or you can pass it as the result of a function (as is here). You can even ignore assigning or returning that function and call the function right away, which leads to [an immediately invoked function expression (IIFE)](https://stackoverflow.com/questions/8228281/what-is-the-function-construct-in-javascript) and you don't leave a reference to the function anywhere. – VLAZ Mar 27 '19 at 08:44
  • 1
    @HeavensGate666 to have a way simpler example to compare against, `2 + 2` is also an expression. It returns a number. You can assign it to a variable `x = 2 + 2`, you can have it as the result of a function `function f() { return 2 + 2; }` and you can even have it by itself on a line `2 + 2;` and it will behave exactly the same as a function expression will - it's still evaluated when reached, it produces a value and then you work with the value. Or throw away the value, if you don't capture or use it somehow. – VLAZ Mar 27 '19 at 08:47
  • @VLAZ oh a math expression ok thank you for the information ! – HeavensGate666 Mar 27 '19 at 09:17

1 Answers1

6

it runs fine but throws an error at the end

Sounds like a linting error, not a Javascript error - the difference is important to keep in mind. Linting is mostly a style guide, rather than a logic guide.

In

return function(y) {
  return x + y;
};

The function there is being returned, not declared - that means it's a function expression, not a function declaration, and so the end of the return expression should have a ;.

Anonymous functions are always function expressions; function declarations require a name, eg:

function someFnName() {
}
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • so everything that is being returned should end in a semicolon? how is it a function expression if it istored in a variable? – HeavensGate666 Mar 27 '19 at 08:14
  • 1
    @HeavensGate666 "*so everything that is being returned should end in a semicolon?*" not necessarily because of [Automatic Semicolon Insertion (ASI)](https://stackoverflow.com/questions/2846283/what-are-the-rules-for-javascripts-automatic-semicolon-insertion-asi) in JavaScript. Omitting the semicolon is not a problem in this case. – VLAZ Mar 27 '19 at 08:16
  • 1
    @HeavensGate666 The interpreter expects a semicolon at the end of every standalone statement or expression, with the exception of `if`, `for`, and related blocks, and function declarations. If you neglect semicolons, the interpreter will attempt to insert the required semicolons for you (see [rules](https://stackoverflow.com/questions/2846283/what-are-the-rules-for-javascripts-automatic-semicolon-insertion-asi)) but this occasionally results in [troubles](https://stackoverflow.com/questions/55347263/destructuring-array-into-an-object), so it's not good to rely on unless you're sure. – CertainPerformance Mar 27 '19 at 08:19
  • @CertainPerformance ok thank you for explaining i will read about ASI – HeavensGate666 Mar 27 '19 at 08:28