Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Function Expressions vs Function Declarations

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 205
    Comment on it

    Every function in JavaScript is an object of Function and in JavaScript, there are many different ways of defining Javascript function. Each of them will do approximately the same thing, but has its own pros and cons. Here we are going to explain how to define  function expression and function declaration and what is the difference between the two.

     

    Function declarations are probably the most familiar and oldest way of defining functions in JavaScript. A Function Declaration defines a named function variable without requiring variable assignment.  

     

    For example:
     

    function multiply(x, y) {
       return x * y;
    }

    A function declaration creates a variable with the same name as the function name. Thus function declarations can be accessed by their name in the scope they were defined in. So the above code will create a variable multiply which is accessible in the current scope.
    Function Declarations are parsed at a pre-execution stage when the browser prepares to execute the code. Which means that we can call the functions before they are written in our code. It won’t matter because the entire function gets loaded to the top of its containing scope.

     

    For example, this will work fine:

    say("John")
    
    function say(name) {
    
      alert("Hello, "+name)
    
    }
    
    //Above code Will gets executed as this:
    
    function say(name) {
    
      alert("Hello, "+name)
    
    }
    
    say("John")

     

    Function expressions: A function expression looks similar to function declarations, except that the function is assigned to a variable name.
    Function expressions are very flexible, and can used in many contexts(as closures,a s arguments to other functions)
    A function expression can be defined in two ways:

    • A function expression of an anonymous function assigned to the variable multiply:
    var multiply = function(x, y) {
       return x * y;
    };

     

    • A function expression of a named function assigned to the variable multiply:
    var multiply = function mul(x, y) {
       return x * y;
    };

     

    Few differences between Function Expressions and Function Declarations:

    • A very important difference between function declaration and function expressions is that a function declaration can be placed anywhere withinin a script because the interpreter reads the script before running it, whereas A function expression, on the other hand, is created during runtime.So if we try to call a function expression before it's loaded, we will get an error but If we call a function declaration it will always work fine.
    • Another difference is that functions declarations must have a name. Which means method won't allow you to create an anonymous functions, meaning that you always have to give it an identifier. whereas function expression can create an anonymous function.
    • Function declaration does not need to be followed with a semicolon. whereas function expressions needs to be followed by a semicolon.

     

     

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: