Write a mul function which will produce the following outputs when invoked:

console.log(mul(2)(3)(4)); // output : 24 
console.log(mul(4)(3)(4)); // output : 48

Below is the answer followed by an explanation to how it works:

function mul (x) {
    return function (y) { // anonymous function 
        return function (z) { // anonymous function 
            return x * y * z; 
        };
    };
}

Here the mul function accepts the first argument and returns an anonymous function, which takes the second parameter and returns another anonymous function that will take the third parameter and return the multiplication of the arguments that have been passed.

In JavaScript, a function defined inside another one has access to the outer function’s variables. Therefore, a function is a first-class object that can be returned by other functions as well and be passed as an argument in another function.

  • A function is an instance of the Object type
  • A function can have properties and has a link back to its constructor method
  • A function can be stored as a variable
  • A function can be pass as a parameter to another function
  • A function can be returned from another function
Asked on January 18, 2019 in Javascript.
Add Comment


  • 4 Answer(s)
    1. console.log(mul(2)(3)(4)); // output : 24
    2.  
    3. function mul (x) {
    4.     return function (y) { // anonymous function
    5.         return function (z) { // anonymous function
    6.             return x * y * z;
    7.         };
    8.     };
    9. }
    Answered on January 19, 2019.
    Add Comment
    console.log(mul(2)(3)(4)); // output : 24
    console.log(mul(4)(3)(4)); // output : 48
    

    Below is code followed by an explanation how it works:

    function mul (x) {
      return function (y) { // anonymous function
        return function (z) { // anonymous function
          return x * y * z;
        };
      };
    }
    

    Here mul function accept the first argument and return anonymous function which take the second parameter and return anonymous function which take the third parameter and return multiplication of arguments which is being passed in successive.

    In Javascript function defined inside has access to outer function variable and function is a first class object so it can be returned by a function as well and passed as the argument in another function.

    • A function is an instance of the Object type
    • A function can have properties and has a link back to its constructor method
    • Function can be stored as variable
    • Function can be pass as a parameter to another function
    • Function can be returned from function
    Answered on January 20, 2019.
    Add Comment

    What is the difference between undefined and not defined in JavaScript?

    In JavaScript, if you try to use a variable that doesn’t exist and has not been declared, then JavaScript will throw an error var name is not defined and script will stop executing. However, if you use typeof undeclared_variable, then it will return undefined.

    Before getting further into this, let’s first understand the difference between declaration and definition.

    Let’s say var x is a declaration because you have not defined what value it holds yet, but you have declared its existence and the need for memory allocation.

    > var x; // declaring x
    > console.log(x); //output: undefined
    Here var x = 1 is both a declaration and definition (also we can say we are doing an initialisation). In the example above, the declaration and assignment of value happen inline for variable x. In JavaScript, every variable or function declaration you bring to the top of its current scope is called hoisting.

    The assignment happens in order, so when we try to access a variable that is declared but not defined yet, we will get the result undefined.

    Answered on January 27, 2019.
    Add Comment

    function mul (x) {
    return function (y) { // anonymous function
    return function (z) { // anonymous function
    return x * y * z;
    };
    };
    }

    Answered on January 27, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.