JAVASCRIPT FUNCTION WITH VARARGS

1.49K viewsProgrammingjavascript programming
1

How to write javascript add function such that add(a)(b) and add(a,b) both return sum ?

Answered question
0

function add(a, b) {
    if (arguments.length === 1) {
        return function(b2) { // inner function which is a closure
            return a + b2; // it has access to the first param a
        };
    }
    return a + b;
}

Answered question
You are viewing 1 out of 1 answers, click here to view all answers.
Write your answer.

Categories