Arrow function
In Arrow function don't need name our functions.
arrow functionconst myFunc = function () {const myVar = "shreyash";return myVar;}
Write Arrow function with parametersconst myFunc = () => {const myVar = "shreyash";return myVar;}
just like a regular function you pass arguments into an arrow function
If there is only one parameter in the arrow function, the brackets connecting the parameters can be omitted.const newFun = (a) => console.log(a);
const newFun = a => console.log(a);
const profile= (name = "shreyash") => "Hello " + name;console.log(profile("pappu"));console.log(profile());
Hello pappu Hello shreyashUse Rest Parameter with function parameters
ES6 provides rest parameter for function parameter to help us create more flexible functions. you can create function that take a variable number of arguments. These arguments are stored in an array that can be accessed later rom inside the function.
function howMany(...args) {return "You have passed " + args.length + " arguments.";}console.log(howMany(0, 1, 2));console.log(howMany("string", null, [1, 2, 3], {}));
You have passed 3 arguments. You have passed 4 arguments.
Post a Comment