0

can i ask why sometimes we create a function, we end up with a semi colon and sometimes does not

function test(){

};

function test(){

}
Marcel Korpel
  • 21,536
  • 6
  • 60
  • 80
dramasea
  • 3,370
  • 16
  • 49
  • 77
  • 1
    Possbile duplicate: http://stackoverflow.com/questions/444080/do-you-recommend-using-semicolons-after-every-statement-in-javascript http://stackoverflow.com/questions/2717949/javascript-when-should-i-use-a-semicolon-after-curly-braces – karthick Apr 04 '13 at 11:43
  • You can find the answer in this stackoverflow posts http://stackoverflow.com/questions/2717949/javascript-when-should-i-use-a-semicolon-after-curly-braces http://stackoverflow.com/questions/444080/do-you-recommend-using-semicolons-after-every-statement-in-javascript http://stackoverflow.com/questions/1834642/best-practice-for-semicolon-after-every-function-in-javascript – Sano Apr 04 '13 at 11:42

3 Answers3

1

Depends on how you declare the function.

var myfunc = function() {}; // use semicolon

function myfunc() {} // don't use semicolon
Tim S.
  • 13,597
  • 7
  • 46
  • 72
  • 1
    The first semicolon is not necessary either in this case. – Esailija Apr 04 '13 at 11:50
  • It's not necessary but you should always put semicolons after (function) expressions. – John Strickler Apr 04 '13 at 11:54
  • I know it's not necessary, but I always try to teach people the best practise. If he would discard the semicolon he will end up with a SyntaxError sooner or later. Because it matters so little, it's best to learn it the *right way*. In fact, if you "learn" both ways, it's actually harder to debug if you ever do come across the SyntaxError because you have learned that both is right. – Tim S. Apr 04 '13 at 11:57
0

check compress following code in http://javascriptcompressor.com/

var test  = function (){

}

a = 10;

its like var test=function(){}a=10; , which is SyntaxError

it will not be a problem for declare function like

function test(){

}

compress code function test(){}a=10; is valid one

rab
  • 4,134
  • 1
  • 29
  • 42
0

To contextualize the answer, you have two scenarios to deal with.

  1. Function declaration - explicitly declaring a function thus:

    function someFunction(){
      // some code
    }
    
  2. Function Expression - declaring the function as an expression thus:

    var someFunction = function(){
      // some code
    };
    

NOTE You only need semicolon in the second case. The difference is important when it comes to the concept of function hoisting.

Function hoisting basically means that you have to put into consideration at what point you want your function to be 'visible' (execution context) at run time. Case 1 enables the function to be visible anywhere within the script to mean irrespective of where you have declared the function within the script, you can always call it. The function is hoisted higher up in the execution context. On the other hand, this does not happen for case 2, so you can only call the function below the line where you have declared it.

gmajivu
  • 1,272
  • 2
  • 13
  • 21