arrows function javascriptt code example

Example 1: arrow function in javascript

//function old way
function PrintName(name){
  console.log("my name is ", name) ;
}
// Arrow function es6
const PrintName =  (name) => {
  return console.log("my name is " , name) ;
}

Example 2: arrow function javascript

//If body has single statement
let myFunction = (arg1, arg2, ...argN) => expression

//for multiple statement
let myFunction = (arg1, arg2, ...argN) => {
    statement(s)
}
//example
let hello = (arg1,arg2) => "Hello " + arg1 + " Welcome To "+ arg2;
console.log(hello("User","Grepper"))
//Start checking js code on chrome inspect option

Example 3: arrow func in javascript

const greet = (who) => {
  return `Hello, ${who}!`;
};

greet('Eric Cartman'); // => 'Hello, Eric Cartman!'

Example 4: how to make javascript function consise

multiplyfunc = (a, b) => { return a * b; }

Example 5: arrow function javascript

// Traditional Function
function (param) {
  var a = param * 3;
  return a;
}

//Arrow Function
(a, b) => {
  let c = (a * b) + 3;
  return c;
}