2

I've come across multiple examples and I'm getting confused about placing semicolon after declaring a function where it acts as a function inside function as shown in the below example

var myFunction = function() {
   var value = "parentValue";   

   function otherFunction() {
      var value = "childValue";
      console.log("Value is : "+value);
   }

   otherFunction();
}

myFunction();

i.e placing the semicolon in the end of the otherFunction() declaration. If I keep ; or not it's working. So which is the best practice?

KTPatel
  • 1,194
  • 4
  • 18
  • 24
Kishore Kumar Korada
  • 1,184
  • 3
  • 19
  • 42
  • Right... if you don't use this, you may get erros when calling functions in javascript, or it will not have enough function names to use and your function names will be more longer, probably it will be more heavy! – Klaider Oct 24 '15 at 09:13
  • Some side information: this kind of function is often called a "closure function" because it inherits the scope of the enclosing function. Also see [Closure (computer programming)](https://en.wikipedia.org/wiki/Closure_(computer_programming)) – Another Code Oct 24 '15 at 09:26
  • See @sachink's answer below and my comment... – An0nC0d3r Oct 24 '15 at 09:26
  • @AdamJeffers: There is no (interesting) closure in the presented code example. – Bergi Oct 24 '15 at 09:37
  • @Bergi Your comment has confused me :-/ – An0nC0d3r Oct 24 '15 at 09:40
  • @AdamJeffers: `otherFunction` does not close over any variables, so it's [not a closure](http://stackoverflow.com/a/30252701/1048572). Notice that `value` is shadowed and `console` is global anyway. – Bergi Oct 24 '15 at 10:19

2 Answers2

7

Function declarations are no statements. They are not terminated by a semicolon, you should not use any.

If you put a semicolon there, it's parsed as an empty statement after the declaration.

However, not all function definitions are statements. myFunction is a variable that is assigned a function expression, and assignments are expression (or variable declaration) statements that are (should be) terminated by a semicolon.

function otherFunction() {
    …
} // <-- no semicolon after declaration

var myFunction = function() {
    …
}; // <-- semicolon after assignment

See also var functionName = function() {} vs function functionName() {} for more differences.

Community
  • 1
  • 1
Bergi
  • 513,640
  • 108
  • 821
  • 1,164
4

Note that the assignment of the anonymous function to myFunction needs a semi colon, no other statement in this scenario is required...

   var myFunction = function() 
   {
       var value = "parentValue"; // declaring a var so use semicolon

       function otherFunction() 
       {
          var value = "childValue"; // declaring a var so use semicolon
          console.log("Value is : "+value)
       } // is not being assigned to anything so no need for semicolon

       otherFunction()

    }; // declaring a var so use semicolon

    myFunction()

And to give an idea how this would work with the module pattern...

var Module = (function(){

    var value = "parentValue";

    function _myFunction()
    {
        _otherFunction()
    } 

    function _otherFunction() 
    {
        var value = "childValue";
        console.log("Value is : "+value)
    }

    return{

        myFunction: _myFunction()
    }; // with semicolon

})(); // with semicolon

Module.myFunction;
An0nC0d3r
  • 1,265
  • 12
  • 29