Quiz, challenge, js, why?, | Sololearn: Learn to code for FREE!


Quiz, challenge, js, why?,

Can someone help and tell me why the output of the code below is 7 ? ---------------------------------------------------------- function abc(a){ return(function(y) { return y+1; }) (++a)+a; } alert(abc(2));

2/18/2018 1:17:20 PM


4 Answers

New Answer


I just didn't know why the returned function is executed...


++2 gives 3, so value of a is now 3. 3+3+1 = 7


You execute your function in return statement. (function(y){ y+1; })(++a) // ++a is an argument of your function You execute it with ++a. Preincrementation works before going into function hence "a" is 3. Then function adds 3+1 which is 4 and then abc function returns result of inner function and adds a, so it gives 4 + 3.


As Jakub said above 😄 you can review this lesson if you want.. https://www.sololearn.com/learn/JavaScript/1130/ Good luck my friend 💚