JavaScript Arrow Operator ( => )
Description
The Arrow Function Operator ( => ) creates functions with a short syntax.
Feature | Regular Function | Arrow Function |
---|---|---|
Syntax | function add(a, b) {
|
const add = (a, b) => a + b; |
this | this is dynamic Depends on how the function is called | this is lexical Inherits from surrounding scope |
new | new myFunction can be used |
new can NOT be used |
hoisting | Are hoisted | Must be defined before use |
arguments | Has an argument object |
Must use rest parameters (...args) |
Use when | You need your own this, arguments, or hoisting | When you want concise functions and lexical this |
More Examples
Example
Arrow function with a single parameter (parentheses can be omitted):
const square = x => x * x;
let result = square(2);
Try it Yourself »
Example
Arrow function with no parameters:
const greet = () => "Hello!";
let result = greet();
Try it Yourself »
Learn More:
Browser Support
=>
is an ECMAScript6 (ES6 2015) feature.
JavaScript 2015 is supported in all browsers since June 2017:
Chrome 51 |
Edge 15 |
Firefox 54 |
Safari 10 |
Opera 38 |
May 2016 | Apr 2017 | Jun 2017 | Sep 2016 | Jun 2016 |