Arrow Function
Categories:
less than a minute
Arrow functions are really helpful when you want create reusable one liner function.
e.g. let us assume you want to add two numbers, then you would normally create a function as follows
function add(a, b) {
return a + b
}
instead of this we can create an arrow function in one line e.g.
const add = (a, b) => a + b
In arrow function there is no need to have explicit return statement. The last calculated or assigned value is automatically returned
Returning object
If you try return the object as follows, it will return undefined, because arrow function assumes the curly branches {} as a block of code rather an object.
const getObject = (val) => { value: val } // wrong
To consider this as an object, this needs to be wrapped with bracket.
const getObject = (val) => ({ value: val })