An arrow function is a shorter syntax for defining a function in JavaScript. It was introduced in ECMAScript 6 (also known as ECMAScript 2015) and is now widely supported in modern browsers and environments.
Here is an example of a traditional function definition:
function add(x, y) {
return x + y;
}
Here is the same function defined using an arrow function:
const add = (x, y) => {
return x + y;
};
You can also define a function with a single expression using the concise body syntax. In this case, the return statement is implicit, and you can omit the curly braces:
const add = (x, y) => x + y;
Arrow functions have a few notable differences from traditional function declarations:
- They do not have their own
this
,arguments
,super
, ornew.target
. - They cannot be used as constructors.
- They do not have a
prototype
property.