What is arrow function in JavaScript with example?

0 votes
23 views
Ravi Vishwakarma asked 13 days ago in Web Application by Ravi Vishwakarma

What is arrow function in JavaScript with example?

1 Answer

0 votes
Ethan Karla answered 11 days ago by Ethan Karla
Arrow functions allow us to write shorter function. It gets shorter If the function has only one statement, and the statement returns a value, you can remove the brackets and the return keyword. If you have parameters, you pass them inside the parentheses. An arrow function expression is a compact alternative to a traditional function expression, but is limited and can't be used in all situations.
1. Does not have its own bindings to this or super, and should not be used as methods.
2. Does not have arguments, or new.target keywords.
3. Not suitable for call, apply and bind methods, which generally rely on establishing a scope.
4. Can not be used as constructors.
5. Can not use yield, within its body.
Syntax:
[variable] = ( [arguments,---] ) => { statement }
Example:
hello = () => {

  return 'Hello World!';
}
hello = () => 'Hello World!';
hello = (val) => 'Hello ' + val;
const materials = [

  'Hydrogen',
  'Helium',
  'Lithium',
  'Beryllium'
];
console.log(materials.map(material => material.length));
let add = (x, y) => { return x + y; };
console.log(add (5,9));