a = 55;
b = 65;
c = 75;
function add(a,b,c){
return a + b + c;
}
console.log(add(55,65,75));
//function expression
let add = function(a,b,c){
return a + b + c;
}
console.log(add(55,65,75));
// using arrow function
let add = (a,b,c) => {
return a+b+c;
}
console.log(add(55,65,75));
Arrow Functions:
1) Arrow functions are a concise syntax for writing function expression in
JavaScript.
2) They provide a shorter and more expressive way to define functions.
3) Arrow function do not have their own this value, and they inherit the this value
from the enclosing context.
syntax:
1) let arrowFunc = (arg1, arg2, ...argN) => {
statement
}
2) let arrowFunc = (arg1, agr2, ...argN) => expression
arrow function with no arguements:
let name = () => console.log("pranesh");
name();
// arrow function with one arguement:
let name = a => console.log(a);
name('pranesh unikaksha');
//Arrow function as an expression:
let age = 25;
let result = (age > 18) ? () => console.log("you are good to go") :
() => console.log("not yet");
result();
//multiple statements arrow functions:
let sum = (a,b) => {
let result = a + b;
return result;
}
let result1 = sum(55,88);
console.log(result1);
let sum1 =(a,b) => a + b;
console.log(sum1(55,88))
this keyword with arrow function:
// inside regular function method:
function person(){
this.name = "pranesh";
this.age = 25;
this.name1 = function(){
console.log(this.name);
function innerFunc(){
console.log(this.age);
console.log(this);
}
innerFunc();
}
}
let a = new person();
a.name1();
// inside a arrow function method:
function person(){
this.name = "pranesh";
this.age = 25;
this.name1 = function(){
console.log(this.name);
let innerFunc = () => {
console.log(this.age);
console.log(this);
}
innerFunc();
}
}
let a = new person();
a.name1();
arguements binding:
regular functions have arguements binding. That is when you pass arguements to a
regular function, you can access themusing the arguements keyword.
arrow function do not have arguements binding.
let a = function(){
console.log(arguments);
}
a(5,6,7);
let a1 = () => {
console.log(arguments);
}
a1(5,6,7);
let a2 = (...n) => {
console.log(n);
}
a2(5,6,7);
*/
0 Comments