// Function in JavaScriptfunctionregular(){console.log("regular function");}regular();//regular function// Arrow Functionconstarrow=()=>console.log("Arrow function");arrow();//Arrow function
// Non Arrow (standard way)letadd=function(x,y){return x + y;}console.log(add(10,20));// 30// Arrow styleletadd=(x,y)=> x + y;console.log(add(10,20));// 30;// You can still encapsulateletadd=(x, y)=>{return x + y;};
// arrow function shorten way to write function//it make easy to write callback functionconst arrowFunction = names.map((name)=>{return name.length<6?"long name":"short name"})//if we have one parameter in callback function we don't need to add parenthesis ()//if there is only one code logic and return value then we can remove return and {}const arrowFunction = names.map(name=> name.length<10?"long name":"short name")
/**
I think that you might be looking for
the js "arrow function"; I hope that
this example below helps ;)
**/// usual functionfunctionfartOne(){console.log('Pooofff... pof.. ppf.. poof.. p');}// arrow function to do the sameconstfartTwo=()=>console.log('Baaaf... paf.. poof.. poffie.. plop');// call the functions to test 'em out..fartOne();fartTwo();
functiondouble(x){return x *2;}// Traditional wayconsole.log(double(2))// 4constdouble=x=> x *2;// Same function written as an arrow function with implicit returnconsole.log(double(2))// 4
// Traditional Functionfunction(a, b){return a + b +100;}// Arrow Function(a, b)=> a + b +100;// Traditional Function (no arguments)let a =4;let b =2;function(){return a + b +100;}// Arrow Function (no arguments)let a =4;let b =2;()=> a + b +100;
var array =[1,2,3,4]constsum=(acc, value)=> acc + value
constproduct=(acc, value)=> acc * value
var sumOfArrayElements = array.reduce(sum,0)var productOfArrayElements = array.reduce(product,1)
constpower=(base, exponent)=>{let result =1;for(let count =0; count < exponent; count++){
result *= base;}return result;};//if the function got only one parameterconstsquare1=(x)=>{return x * x;};constsquare2=x=> x * x;// empty parameterconsthorn=()=>{console.log("Toot");};
// Arrow functions let us omit the `function` keyword.// Here `long_example` points to an anonymous function value.constlong_example=(input1, input2)=>{console.log("Hello, World!");const output = input1 + input2;return output;};// If there are no braces, the arrow function simply returns the expression// So here it's (input1 + input2)constshort_example=(input1, input2)=> input1 + input2;long_example(2,3);// Prints "Hello, World!" and returns 5short_example(2,5);// Returns 7// If an arrow function only has one parameter, the parentheses can be removed.constno_parentheses=input=> input +2;no_parentheses(3);// Returns 5
functionA(){this.val="Error";(function(){this.val="Success";})();}functionB(){this.val="Error";(()=>{this.val="Success";})();}var a =newA();var b =newB();
a.val// "Error"
b.val// "Success"
// Arrow function with two arguments constsum=(firstParam, secondParam)=>{return firstParam + secondParam;};console.log(sum(2,5));// Prints: 7 // Arrow function with no arguments constprintHello=()=>{console.log('hello');};printHello();// Prints: hello// Arrow functions with a single argument constcheckWeight=weight=>{console.log(`Baggage weight : ${weight} kilograms.`);};checkWeight(25);// Prints: Baggage weight : 25 kilograms.// Concise arrow functionsconstmultiply=(a, b)=> a * b;console.log(multiply(2,30));// Prints: 60
// Defining an anonymous arrow expression that simply logs a string to the console.console.log(()=>console.log('Shhh, Im anonymous'));// Defining a named function by creating an arrow expression and saving it to a const variable helloWorld. consthelloWorld=(name)=>{console.log(`Welcome ${name} to Codecademy, this is an arrow expression.`)};// Calling the helloWorld() function.helloWorld('Codey');//Output: Welcome Codey to Codecademy, this is an Arrow Function Expression.
// An empty arrow function returns undefinedletempty=()=>{};(()=>'foobar')();// Returns "foobar"// (this is an Immediately Invoked Function Expression)varsimple=a=> a >15?15: a;simple(16);// 15simple(10);// 10letmax=(a, b)=> a > b ? a : b;// Easy array filtering, mapping, ...var arr =[5,6,13,0,1,18,23];var sum = arr.reduce((a, b)=> a + b);// 66var even = arr.filter(v=> v %2==0);// [6, 0, 18]var double = arr.map(v=> v *2);// [10, 12, 26, 0, 2, 36, 46]// More concise promise chains
promise.then(a=>{// ...}).then(b=>{// ...});// Parameterless arrow functions that are visually easier to parsesetTimeout(()=>{console.log('I happen sooner');setTimeout(()=>{// deeper codeconsole.log('I happen later');},1);},1);