0

My javascript code works with arrow function but not with normal function

//NORMAL FUNCTION (not working)
function multiplier(factor) {
    return function (number) {
        number * factor;
    }
}
const twice = multiplier(2);
console.log(twice(5));


//ARROW FUNCTION (working)
function multiplier(factor) {
    return number => number * factor;
}
const twice = multiplier(2);
console.log(twice(5));

Thanks

2
  • 2
    You need to return result in return function (number) { and with arrow function without curly braces result is returned automatically. Commented Sep 11, 2020 at 7:53
  • See: stackoverflow.com/questions/34361379/… Commented Sep 11, 2020 at 7:55

1 Answer 1

4

You're missing a return statement in the inner function

function multiplier(factor) {
    return function (number) {
        return number * factor;
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

So the return before the normal function is not doing anything?
Nice catch! After the fix, multiplier(2)(5) can be used
@HamzaMasood it does, it returns the inner function. If you use an arrow function without braces it just returns automatically. You can also write it as a double arrow function const multiplier = (factor) => (number) => factor * number, then you don't need the return statement on the outer function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.