+ 1

Explain this code

function abc(a){ return (function (y){ return y+1; }) (++a)+a; } console.log(abc(2));

2nd Feb 2021, 4:37 AM
CHANDAN ROY
CHANDAN ROY - avatar
1 Answer
+ 13
1. abc function is declared. 2. Then we call it and pass 2 as an argument. 3. Inside of abc function parameter a is incremented (it becomes 3) and passed to an immediately called anonymous function (so called IIFE). 4. Then inside of the anonymous function y+1 is evaluated (3+1=4) and returned. 5. Then parameter a (which is 3) is added to the returned value of the anonymous function (which is 4) 6. As a result abc function returns 7 and it is passed to console.log() 7. So, the console output will be 7.
2nd Feb 2021, 5:59 AM
Igor Makarsky
Igor Makarsky - avatar