What is the difference between the two?

I saw that the following JavaScript functions work in exactly the same way. Then what is the difference between them except the syntax . Function:

var functionName=function(){ //some code here }; function functionName(){ //some code here } 

I call them the same way as:

 functionName(); 

Please do not tell me that the syntax is different there, but there is some difference, for example

 1)speed of execution 2)Memory utilization etc. 

Thanks in advance!

+7
source share
2 answers

This has been answered many times in StackOverflow. This is just a naming convention. Therefore, taking a few answers to the questions, I would say:

  • Function declarations and variable declarations always move (β€œraised”) invisibly to the top of their area using a JavaScript interpreter. Functional parameters and language names obviously already exist.

  • Advantages and disadvantages:

    There are several advantages of naming functions:

    • meta-analysis names. functionInstance.name will show you the name.
    • More importantly, the name will be printed in the stack trace.
    • names also help you write down self-documenting or literate code.

    There is one drawback of these function expressions

    • IE has memory leaks for NFE
  • Another major difference

    The difference is that functionTwo is defined at parsing time for the script block, while functionOne is defined at runtime. For example:

     <script> // Error functionOne(); var functionOne = function() { } </script> <script> // No error functionTwo(); function functionTwo() { } </script> 

References

+1
source
  • 1st β€” Named expressions of functions that must return a specific value to the caller.
  • Part 2 is just a function, it depends on whether you return a value or not.
0
source

All Articles