The JavaScript Arrow function helps reducing the number of lines in a JavaScript function you like.
Hey! I’m Yogi. I created www.yogihosting.com and I write tutorials on web development to help beginners better understand the inner workings of Web Development.
If you have any questions about the article, leave a comment and I’ll get back to you, or find me on twitter @yogihosting.
Take for example:
1. Finding a square of a number
This is what you will create a Squaring function in JavaScript:
function SquareFunc(num)
{
return num * num;
}SquareFunc(3)
The above JavaScript function can be created with just 1 line with JavaScript Arrow Function. See this:
var funcName = (num) => num * num
funcName(5);
The jQuery Each method is an amazing way to reduce number of lines of your code. It is mainly used in DOM Traversal.
2. Multiplying 2 numbers
In this case we will have 2 parameters that’s why our arrow function will look like:
var funcMulti = (num1,num2) => num1 * num2
funcMulti(3,5);
3. Function with no parameter
If the function has no parameter the arrow function would be like this:
var showAlert = () => alert(‘Hello!’);
showAlert();
4. Function with Multiple Expressions
When we have multiple expressions then our arrow function can have curly brackets {…}, this is how it will look like:
var showAlert = () => {
var currTime = Date()
alert(currTime)
};showAlert()
Conclusion
I hope you loved the Arrow Function of JavaScript and this tutorial should give you a great base of knowledge for further learning.
As always, please do some clapping for me & leave a comment for any of your through regarding jQuery.