PASSWORD RESET

Your destination for complete Tech news

What are arrow functions in Javascript?

276 0
< 1 min read

Arrow functions are a syntax for defining anonymous functions (functions without a name) in JavaScript. They were introduced in ECMAScript 6 (also known as ECMAScript 2015) and have a concise syntax compared to regular functions.

Here is an example of a regular function in JavaScript:

function sum(a, b) {
  return a + b;
}

Here is the same function written using an arrow function:

const sum = (a, b) => a + b;

As you can see, the arrow function syntax is much shorter and easier to read than the regular function syntax.

Arrow functions have a few other notable features:

  • They do not have their own this value. Instead, they inherit the this value of the surrounding context.
  • They do not have a arguments object. Instead, you must use the rest operator (...) to access the arguments passed to the function.
  • If the function body consists of a single statement, you can omit the curly braces and the return keyword.

Here is an example of an arrow function that uses the rest operator and omits the curly braces and return keyword:

const sum = (...numbers) => numbers.reduce((a, b) => a + b);

Arrow functions are a useful tool for writing concise, functional code in JavaScript. They are commonly used with array methods like map, filter, and reduce, as well as in event handlers and other asynchronous code.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.