console.log(2 + function() {return 3} ()); // equals 5
So this was one of the exercises in Codecademy’s practice pack for js functions. I get that the function runs while in console log but what does the set of parenthesis after it mean?
console.log(2 + function() {return 3} ()); // equals 5
So this was one of the exercises in Codecademy’s practice pack for js functions. I get that the function runs while in console log but what does the set of parenthesis after it mean?
console.log(2 + function() {return 3} () ); // equals 5
That last set of parenthesis is what is telling JavaScript to call the anonymous function. This can be demonstrated further by giving the function a parameter, so we can see that the argument given inside the parenthesis is passed to it.
console.log(2 + function(num) {return num * 2} (3) ); // 8 would be printed to the console
3 is passed to the function, the function doubles it and returns the value.
Not a clear coding style, but the concept demonstration is there.
Thank you so much! It makes perfect sense now
Wow this is gold, thank you!