"Arrow" is the short-hand way of writing function. Before it, there was a much less-used operator in JS that would be handy way of writing "goes to" but that was not so popular as the arrow function.
- function countdown(n) {
- while (n --> 0)
- alert(n);
- }
By running this script, the 'n' value is decremented till it becomes zero. It is short hand nortation of the below code.
In the same way, the ==> function in JavaScript can be used to write the function.
-
- var selected = ArrayObj.filter(function (item) {
- return item.toUpperCase()
- });
-
-
- var selected = ArrayObj.filter(item=>item.toUpperCase());
The syntax for arrow function is
- (param1, param2, …, paramN) => expression
-
-
- ()==>{return statement;}
This way of notation is very useful for writing functions in making filter,s maps, or performing any operation in array object along with doing small manipulations.
-
- let empty = () => {};
-
- (() => 'foobar')();
-
-
-
- var compareVar= a => a > 15 ? 15 : a;
- compareVar(16);
- compareVar(10);
-
- let max = (a, b) => a > b ? a : b;
-
-
-
- var arr = [5, 6, 13, 0, 1, 18, 23];
-
- var sum = arr.reduce((a, b) => a + b);
-
-
- var even = arr.filter(v => v % 2 == 0);
-
-
- var double = arr.map(v => v * 2);
-
Next time, for writting an inline function, the arrow operator can be used.