ECMA Script 6 | Arrow Functions =>

Arrow Functions in ECMA Script 6:

Introduction:

Arrow functions allow us to store functions as Constants and Variables.

Body:

The following piece of code allows us to save an arrow function that squares 10 as a const:

const square_function = (x)=>x*x;

We can better understand what is being expressed in the above statement by saying it aloud in English:

square_function is an identifier, which represents an address in memory, and which names the data contained within this piece of memory, and which in this instance is a constant – i.e. its value cannot change at runtime[1] – which contains a function that takes a parameter, x, and which applies a squaring operation to this parameter.

When it comes to the mathematics of functions, it is more proper to say that a function is applied to a parameter.

Below is a button that employs the attribute onclick so as to square 10 and then to print the result or square to the screen:

onclick='document.getElementById("demo").innerHTML=""+square_function(10);'

The above button employs the following piece of HTML/JavaScript code so as to have the function, square_function applied to the argument, 10, and then to have the result or return value of this function printed to the screen:

<p><button type="button" onclick='document.getElementById("demo").innerHTML=""+square_function(10);'><button></p>

<p id="demo"></p>

When the above button is pressed, the above-described code causes the number 100 to be printed to the screen.

Conclusion:

I really enjoy the functional aspects of the more recent versions of Javascript such as ECMA script 6.



1,222 0 12