we will convert this function into arrow function code, and arrow function is the modern way to write code.
In ES6, arrow functions are the compact syntax for functions, it is just like abbreviation for functions and it is recommended that you should harness the power of arrow function,without dealing with the hoisting, arrow functions are considered as Function Expressions, that a lot look like function definition.
It is the main advantage of arrow functions are not hoisted to the top of the scope ,and because the function expresions which are objects, we can't hoist the objects, and we recomend you to create the function object using the const keyword.
const square=(number)=>{return number*number;}
This syntax is similar to
mathematical notation => (βarrowβ) that separate the function parameters from the function body. We don't employed the function keyword, we can pass the parameters as agrugment inside the curly brashes and in case of one single statement,for returning we can omit the return keyword, small brackets and the curly brashes to.
const square=number=>number*number;
Arrow functions are also leveraged to pass as a call back function to another function, for example, you want to sort the array of numbers into increasing(ascending order) , you need to pass a arrow comparision function for this purpose.
Lexical this means arrow function don't have thier own this binding. If we want to double the values which are present on price array,we can use the arrow function inside the map function.
let price=[50,100,200,300];
const two=price.map(price=>price*2);
To understand the lexical this,we will create an object named "bhulbhuleya";
const bhulbhuleya={
city:"Lucknow",
hello(){
console.log("Hello",this.city)
}
};
bhulbhuleya.hello();







Top comments (0)