0

I am almost new to JavaScript, and I am learning about the use of the anonymous function. I have written this piece of code and test it.

Code 1:

function build(something) {
    return function(name) {
        alert("Here is " + something + "\nHello " + name + "!");
    };
}

After that I just removed the ;, but I am getting the same result. Code 2:

function build(something) {
    return function(name) {
       alert("Here is " + something + "\nHello " + name + "!");
    }
}

The way I am calling the function is (for both cases):

var station = build("Station");
station();

Are they equivalent because in any case I am getting any error? If so, why is not needed the semicolon, and which one is mostly used?

lmiguelvargasf
  • 51,786
  • 40
  • 198
  • 203

1 Answers1

2

Semicolons are automatically inserted in certain situations.

Docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Automatic_semicolon_insertion

Transcendence
  • 2,456
  • 3
  • 19
  • 31