How can I make var a = add(2)(3); //5 work?

I want to make this syntax possible:

``````var a = add(2)(3); //5
``````

based on what I read at http://dmitry.baranovskiy.com/post/31797647

I've got no clue how to make it possible.

You need add to be a function that takes an argument and returns a function that takes an argument that adds the argument to add and itself.

``````var add = function(x) {
return function(y) { return x + y; };
}
``````

1.)

`````` function add(a){ return function (b){return a+b;} }

``````

2.)

``````function add(a,b){
var ddd = function (b){return a+b;};
if(typeof b =='undefined'){
return ddd;
}else{
return ddd(b);
}
}

``````

ES6 syntax makes this nice and simple:

``````const add = (a, b) => a + b;

// output: 7

const add2 = a => b => a + b;

// output: 7
``````

It's about JS curring and a little strict with `valueOf`:

``````function add(n){
};

return n;
};

}

``````

It works like a charm with an unlimited adding chain!!

in addition to what's already said, here's a solution with generic currying (based on http://github.com/sstephenson/prototype/blob/master/src/lang/function.js#L180)

``````Function.prototype.curry = function() {
if (!arguments.length) return this;
var __method = this, args = [].slice.call(arguments, 0);
return function() {
return __method.apply(this, [].concat(
[].slice.call(args, 0),
[].slice.call(arguments, 0)));
}
}

return (function (x, y) { return x + y }).curry(x)
}

``````

This will handle both

``````add(2,3) // 5
``````

or

``````add(2)(3) // 5
``````

This is an ES6 curry example...

``````const add = (a, b) => (b || b === 0) ? a + b : (b) => a + b;
``````

Arrow functions undoubtedly make it pretty simple to get the required result:

``````const Sum = a => b => b ? Sum( a + b ) : a;

console.log(Sum(3)(4)(2)(5)()); //14

console.log(Sum(3)(4)(1)()); //8
``````

This is a generalized solution which will solve add(2,3)(), add(2)(3)() or any combination like add(2,1,3)(1)(1)(2,3)(4)(4,1,1)(). Please note that few security checks are not done and it can be optimized further.

``````function add() {
var total = 0;

function sum(){
if( arguments.length ){
var arr = Array.prototype.slice.call(arguments).sort();
return sum;
}
else{
}
}

if(arguments.length) {
var arr1 = Array.prototype.slice.call(arguments).sort();
return sum(mytotal);
}else{
return sum();
}

var x = 0;
for (var i = 0; i < arr.length; i++) {
x = x + arr[i];
};
return x;
}
}

Concept of CLOSURES can be used in this case.
The function "add" returns another function. The function being returned can access the variable in the parent scope (in this case variable a).

``````function add(a){

return function(b){
console.log(a + b);
}

}

``````

Here is a link to understand closures http://www.w3schools.com/js/js_function_closures.asp

With ES6 spread `...` operator and `.reduce` function. With that variant you will get chaining syntax but last call `()` is required here because function is always returned:

``````function add(...args) {
if (!args.length) return 0;
const result = args.reduce((accumulator, value) => accumulator + value, 0);
const sum = (...innerArgs) => {
if (innerArgs.length === 0) return result;
};
return sum;
}

// it's just for fiddle output
document.getElementById('output').innerHTML = `
`;``````
``<div id='output'></div>``

function add() { var sum = 0;

``````    function add() {
for (var i=0; i<arguments.length; i++) {
sum += Number(arguments[i]);
}
}
return parseInt(sum);
};
}

// ...

``````
``````function A(a){
return function B(b){
return a+b;
}
}
``````

I found a nice explanation for this type of method. It is known as Syntax of Closures

``````const add = a => b => b ? add(a+b) : a;

``````

Or `(`\${a} \${b}`)` for strings.

Simply we can write a function like this

``````    function sum(x){
return function(y){
return function(z){
return x+y+z;
}
}
}

sum(2)(3)(4)//Output->9
``````

Don't be complicated.

``````var add = (a)=>(b)=> b ? add(a+b) : a;
``````

it will work in the latest javascript (ES6), this is a recursion function.

Here we use concept of closure where all the functions called inside main function iter refer and udpate x as they have closure over it. no matter how long the loop goes , till last function , have access to x.

``````function iter(x){
return function innfunc(y){
//if y is not undefined
if(y){
//closure over ancestor's x
x = y+x;
return innfunc;
}
else{
//closure over ancestor's x
return x;
}
}
}
``````

iter(2)(3)(4)() //9 iter(1)(3)(4)(5)() //13

we can do this work using closure.

``````    function add(param1){
return param2 = param1 + param2;
}
}
``````
``````let total = 0;
const add = (n) => {
if (n) {
total += n;
}
}

console.log(total);
``````

Is this wrong?

``````let add = (a,b)=>b===undefined ?add.bind(null,a):a+b;
``````

I came up with nice solution with closure, inner function have access to parent function's parameter access and store in its lexical scope, when ever we execute it, will get answer

``````    const Sum = function (a) {
return function (b) {
return b ? Sum(a + b) : a;
}
};

Sum(1)(2)(3)(4)(5)(6)(7)() // result is 28
Sum(3)(4)(5)() // result is 12
Sum(12)(10)(20) // result is 42
``````

enter image description here

``````function add(x) {
return function(y) {
return x + y;
};
}
``````

Ah, the beauty of JavaScript

This syntax is pretty neat as well

``````function add(x) {
return function(y) {
if (typeof y !== 'undefined') {
x = x + y;
return arguments.callee;
} else {
return x;
}
};
}
``````
``````function add(x){
return function(y){
return x+y
}
}
``````

First-class functions and closures do the job.

``````function add(n) {
sum = n;
const proxy = new Proxy(function a () {}, {
get (obj, key) {
return () => sum;
},
sum += args[1][0];
return proxy;
},
});
return proxy
}
``````

Works for everything and doesn't need the final () at the end of the function like some other solutions.

``````console.log(add(1)(2)(3)(10));    // 16
``````
``````function add(a, b){
return a && b ? a+b : function(c){return a+c;}
}

``````

``````let multi = (a)=>{
return (b)=>{
return (c)=>{
return a*b*c
}
}
}
multi (2)(3)(4) //24``````

``````let multi = (a)=> (b)=> (c)=> a*b*c;
multi (2)(3)(4) //24``````

``````function add () {
var args = Array.prototype.slice.call(arguments);

var fn = function () {
var arg_fn = Array.prototype.slice.call(arguments);
}

fn.valueOf = function () {
return args.reduce(function(a, b) {
return a + b;
})
}

return fn;
}

This is concept of currying in JS.

``````function add(a) {
return function(b) {
return a + b;
};
}
``````

This can be also achieved using arrow function:

``````let add = a => b => a + b;
``````

``````function add(a) {
return function(b){
return b ? add(a + b) : a;
}
}
``````

Using ES6 Arrow function Syntax:

``````let add = a => b => b ? add(a + b) : a;
``````

This question has motivated so many answers already that my "two pennies worth" will surely not spoil things.

I was amazed by the multitude of approaches and variations that I tried to put "my favourite" features, i. e. the ones that I would like to find in such a currying function together, using some ES6 notation:

``````const add=(...n)=>{
const vsum=(a,c)=>a+c;
n=n.reduce(vsum,0);
fn.toString=()=>n;
return fn;
}

console.log(w()) // 3
console.log(w); // 3
console.log(w(6)(2,3)(4)); // 18
console.log(w(5,3)); // 11
``.as-console-wrapper {max-height:100% !important; top:0%}``
You can use as many arguments in as many currying levels you want and the result will be another function that can be reused for the same purpose. I used a little "trick" to also get a numeric value "at the same time": I redefined the `.toString()` function of the inner function `fn`! This method will be called by Javascript whenever the function is used without an arguments list and "some value is expected". Technically it is a "hack" as it will not return a string but a number, but it will work in a way that is in most cases the "desired" way. Give it a spin!