JavaScript arrow functions

What is an arrow function in JavaScript?
Arrow functions are an alternative way of writing functions in JavaScript. There are a few limitations compared to the traditional function that will be discussed in this article.

// Traditional function decl…


This content originally appeared on DEV Community and was authored by Aden Eilers

What is an arrow function in JavaScript?
Arrow functions are an alternative way of writing functions in JavaScript. There are a few limitations compared to the traditional function that will be discussed in this article.

// Traditional function declaration
function myFunc(myParam) {
  return myParam + 1;
}

// Arrow function declaration
const myFunc = (myParam) => {
  return myParam + 1;
}

Arrow functions can often be shortened substantially.

// If there is one parameter, then you do not need to include
// the parenthesis. 
const myFunc = myParam => { return myParam + 1; }

// If the functions contents can fit on one line, then you can
// remove the 'return' keyword and '{}'. 
// In this case, the 'return' is implied.
const myFunc = myParam => myParam + 1;

It is common to use arrow functions with the built in array methods.

const arr = [1, 2, 3, 4, 5];

const arrMap = arr.map(v => v + 1);

const arrFiltered = arr.filter(v => v > 3);

arr.forEach(v => console.log(v));

The limitations of Arrow functions.

Arrow functions do not have their own binding of this. As a result, you generally will not want to use them as methods in your objects. A few more things to know:

  • They don't have their own binding to arguments, or super
  • They cannot be used as constructors within classes.
  • They likely will not behave as expected with the keywords call, apply and bind.

Since the arrow function does not have its own binding of this, it cannot reference other attributes or methods in objects. this in an arrow function will always reference the global scope.

const obj = {
  value: 1,
  myFunc1: function () {
    console.log(this.value, this);
  },
  myFunc2: () => {
    console.log(this.value, this);
  },
};

obj.myFunc1(); // 1, obj{...}
obj.myFunc2(); // undefined, Window{...} or the Global object

For more info, reference the MDN documentation: MDN Docs

Leave a comment if you have any questions or feedback.


This content originally appeared on DEV Community and was authored by Aden Eilers


Print Share Comment Cite Upload Translate Updates
APA

Aden Eilers | Sciencx (2022-04-24T23:53:44+00:00) JavaScript arrow functions. Retrieved from https://www.scien.cx/2022/04/24/javascript-arrow-functions/

MLA
" » JavaScript arrow functions." Aden Eilers | Sciencx - Sunday April 24, 2022, https://www.scien.cx/2022/04/24/javascript-arrow-functions/
HARVARD
Aden Eilers | Sciencx Sunday April 24, 2022 » JavaScript arrow functions., viewed ,<https://www.scien.cx/2022/04/24/javascript-arrow-functions/>
VANCOUVER
Aden Eilers | Sciencx - » JavaScript arrow functions. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/04/24/javascript-arrow-functions/
CHICAGO
" » JavaScript arrow functions." Aden Eilers | Sciencx - Accessed . https://www.scien.cx/2022/04/24/javascript-arrow-functions/
IEEE
" » JavaScript arrow functions." Aden Eilers | Sciencx [Online]. Available: https://www.scien.cx/2022/04/24/javascript-arrow-functions/. [Accessed: ]
rf:citation
» JavaScript arrow functions | Aden Eilers | Sciencx | https://www.scien.cx/2022/04/24/javascript-arrow-functions/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.