What is arrow function in javascript

How to work fat arrow function?
An arrow function is the short syntax of the javascript function. Arrow function has no any reserved keyword as function, curly brace and return keyword. Also, we call it a lambda function. Some people call it fat arrow function.  It supports only on the latest version because of this function related to the ES6 (ECMAScript 6).
const ab = (a, b) => a + b;
ab(100200);
// Output: 300
  
If you don’t have any parameters then you need parenthesis() as:
() => { statements }

Also we don’t use parenthesis() in single parameters.
const ab = a => a+2;
ab(100);
// Output: 102

But we should be always used with () and {} because you will be not used always single parameter and then you will be confused and getting errors.
const add = (a, b) => {
  return a + b;
}
add(100200);
// Output: 300

Arrow function example with string:
const mytext = () => {
  return 'Hello ECMAScript 6';
}
mytext();
// Output: Hello ECMAScript 6

Arrow function example with object Type:
Syntax:
=> ( { b: a } )

Example:
const fruits = (name, color) => ({ name: name, color: color });
fruits('Mango''Orange');
// Output: Object {name: "Mango", color: "Orange"}

No comments:

Note: Only a member of this blog may post a comment.

Copyright Reserved to Anything Learn. Powered by Blogger.