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;
// 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;
// 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;
// Output: 300

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

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

const fruits = (name, color) => ({ name: name, color: color });
// 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.