0
What is the meaning of below statement/code?
Console.log(x=>x*(y=>y*2)(2))(2) //output is 8 What's the meaning of '=>' ?
2 Answers
+ 1
The => is a symbol used in arrow functions https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
You might also be interested in reading about IIFE https://developer.mozilla.org/en-US/docs/Glossary/IIFE
+ 1
We have the expression (x=>(y=>y*2)(2))(2).
We start from the inside and work our way out:
y=>y*2 is a function which multiples its argument y by 2.
Adding (2) at the end of this function means to call it with the argument 2. (Look up anonymous functions amd closures).
(y=>y*2)(2) is therefore equal to 4. We are left with:
(x=>x*4)(2)
x=>x*4 is a function which multiples the argument by 4.
(2) means to call this funcyion with the argument 2.
The result is thus 2*4=8