Can you assign an anonymous function to a variable and pass it as an argument to another function? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 4

Can you assign an anonymous function to a variable and pass it as an argument to another function?

var x = function() { return 'It is anonymous function'; };

21st Mar 2021, 10:42 PM
Apongpoh Gilbert
Apongpoh Gilbert - avatar
5 Réponses
+ 3
Why don't you just try it. Make a couple of simple functions. Pass 1 to the other and call the passed function within the body of the second.
21st Mar 2021, 10:49 PM
ChaoticDawg
ChaoticDawg - avatar
+ 3
That's not passing the function x to the function normale, but passing the return value of x to the function normale. Try, let x = function() { return "It's an anonymous function"; }; function normale(func) { console.log(func()); } normale(x);
21st Mar 2021, 11:08 PM
ChaoticDawg
ChaoticDawg - avatar
+ 2
Ok thanks ChaoticDawg
21st Mar 2021, 11:21 PM
Apongpoh Gilbert
Apongpoh Gilbert - avatar
+ 1
Yup
21st Mar 2021, 10:51 PM
D_Stark
D_Stark - avatar
+ 1
Like this? var x = function() { return 'It is anonymous function'; }; var y = x(); console.log(y); function normale(y) { return y; } console.log(normale(y));
21st Mar 2021, 10:59 PM
Apongpoh Gilbert
Apongpoh Gilbert - avatar