Hello Trailblazers,
Welcome to the third tutorial in JavaScript Tutorial Series by SFDC Stop. In this tutorial, we're going to learn about ES6 Fat Arrow Functions in JavaScript. We're going to start from the beginning and see how we can define a function in JavaScript and finally we'll reduce that function to a single line of code using Fat Arrow sytnax. This tutorial is published on SFDC Stop YouTube Channel. So, if you want to learn in detail, have a look at the tutorial video down below. Otherwise, you can scroll down to have a look at the code gist with explanation.
Tutorial Video
Code Gist with Explanation
Q. Write a function to calculate the perimeter of a circle in JavaScript?
The basic syntax to define a function in JavaScript is given below where we're defining a function to calculate the perimeter of a circle.
As you can see in the above code, we have defined a function which is taking radius of a circle as a perimeter. Inside this function, we've a constant named as PI with value 3.14 and finally we're returning the perimeter of a circle which is nothing but 2 * PI * radius. Upon running the above code snippet which is calling the function with the value of radius as 10, the output we're getting is shown below:
As you can see above, we're getting the perimeter of the circle with radius 10 as 62.8. This is how you can Define a Simple Function in JavaScript. Now, let's update this code and this time we're going to assign a function to a variable and call a function afterwards. This is nothing but Another way of defining a function in JavaScript, as given below:
Here we're defining a function using the function keyword but instead of providing a name for the function after the function keyword, we're assigning that function to a variable with name perimeterOfACircle2 and using that variable to call this function in console.log statement. The output we're getting is shown below:
The answer to this question is, YES! You can. Let's have a look at the updated code snippet below, where we've reduced this function code to a single line:
- Following the ES6 syntax we have to keep the parameters to the left hand side and the function body to the right hand side of the fat arrow =>
- While reducing the function to a single line, you need to have the return value of the function to the right hand side of the fat arrow. Notice that here we've removed the constant PI but instead used the value 3.14 directly in the calculation followed by the fat arrow which will be returned. Also, we don't need to specify the return keyword here.
- If you only have a single parameter passed inside your function, you don't need to cover it by paranthesis () you can simply keep it as is. You can see we've removed the paranthesis from the radius here.
- Basically, we're reducing the function body to a single line which is being returned and specifying that line after the fat arrow.
To summarize, the learnings are given below:-
- We can initialize a function by using the function keyword or by using the fat arrow =>
- While using a fat arrow, we need to keep the parameters to the left side of the fat arrow symbol => and the function body to the right side of the fat arrow symbol.
- If we can reduce the function body to one line, we can define it in a single line using fat arrow syntax by just keeping the return value after the fat arrow. In this case the return keyword is not mentioned.
- If we have a single parameter, we can remove the paranthesis () as well but we do need to specify the paranthesis otherwise.
No comments:
Post a Comment