// Normal Function in JavaScript
function Welcome(){
console.log("Normal function");
}
// Arrow Function
const Welcome = () => {
console.log("Normal function");
}
// Function in JavaScript
function regular(){
console.log("regular function");
}
regular(); //regular function
// Arrow Function
const arrow = () => console.log("Arrow function");
arrow(); //Arrow function
// Non Arrow (standard way)
let add = function(x,y) {
return x + y;
}
console.log(add(10,20)); // 30
// Arrow style
let add = (x,y) => x + y;
console.log(add(10,20)); // 30;
// You can still encapsulate
let add = (x, y) => { return x + y; };
// The usual way of writing function
const magic = function() {
return new Date();
};
// Arrow function syntax is used to rewrite the function
const magic = () => {
return new Date();
};
//or
const magic = () => new Date();
/**
I think that you might be looking for
the js "arrow function"; I hope that
this example below helps ;)
**/
// usual function
function fartOne(){
console.log('Pooofff... pof.. ppf.. poof.. p');
}
// arrow function to do the same
const fartTwo = () => console.log('Baaaf... paf.. poof.. poffie.. plop');
// call the functions to test 'em out..
fartOne();
fartTwo();
function double(x) { return x * 2; } // Traditional way
console.log(double(2)) // 4
const double = x => x * 2; // Same function written as an arrow function with implicit return
console.log(double(2)) // 4
// Traditional Anonymous Function
function (a, b){
return a + b + 100;
}
// Arrow Function
(a, b) => a + b + 100;
// Traditional Anonymous 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;
// Traditional Function
function (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;
//arrow function
()=>{}
//normal function
function(){}
//useses of arrow function
var fnct=()=>{}
var fnct=(param1,param2,...rest)=>{console.log(param1),alert(param2),return(rest)}
//or these
var fnct=e=>{}
var fnct=(e)=>e
var fnct=e=>e
//examples
var fnct=param=>{return 'hello '+param}
var fnct=(param1,param2,...rest)=>!param1?param2:rest
var fnct=return_=>return_
var fnct=hi=>alert(hi)
// Traditional Function
function myFunction(param) {
var a = param * 3;
return a;
}
//Arrow Function
let myFunction = (a, b) => {
let c = (a * b) + 3;
return c;
}
// Traditional Anonymous Function
function (a, b){
let chuck = 42;
return a + b + chuck;
}
// Arrow Function
(a, b) => {
let chuck = 42;
return a + b + chuck;
}