Continuing with my series on JavaScript 6 for the C# developer, it’s time to introduce you to the Arrow Function. Arrow functions provide a way to implement lexical scoping of the this keyword. In other words, the variables may only be referenced from within the code block that they are defined. Let’s begin with an example in C# and then see how we would represent the same thing in JavaScript 6.
All we are doing here is, taking an array of integers 1 to 5 and looping through all these values, taking each value and adding it to itself. The console will as a result log the values 2 4 6 8 and 10. So how do we represent this syntactically similar in JavaScript 6?
OK, I’ll admit that before I introduce you to the JavaScript representation, there is a slight variation in the syntax: the map function. Without diving into the underlying behaviour, map is essentially doing the same thing as the ForEach list type extension.
Pretty simplistic don’t you think? Once again, there isn’t too much difference between the 2 implementations and further strengthens how little that paradigm shift can be.
Thanks for reading and keep building awesome.
Until next time!
Ian B
P.S. You can also find me on Twitter: @ianaldo21