// 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; };
// arrow function shorten way to write function
//it make easy to write callback function
const 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 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
/*The arrow functions were introduced in ECMA 2015 with the main purpose of giving a shorter syntax to a function expression.
Besides providing shorter syntax, which increases the readability of the code,
it does not have its own value of the this object. The value of this object inside an arrow function is inherited from the enclosing scope.
You can write an arrow function to add two numbers as shown in the next code example.*/
var add = (num1, num2)=> num1+num2;
let res = add(5,2);
console.log(res); // 7
// 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;
}
// Arrow functions let us omit the `function` keyword.
// Here `long_example` points to an anonymous function value.
const long_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)
const short_example = (input1, input2) => input1 + input2;
long_example(2, 3); // Prints "Hello, World!" and returns 5
short_example(2, 5); // Returns 7
// If an arrow function only has one parameter, the parentheses can be removed.
const no_parentheses = input => input + 2;
no_parentheses(3); // Returns 5
// 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.
const helloWorld = (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.