From the course: JavaScript: Best Practices for Functions and Classes

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Use arrow syntax for anonymous functions

Use arrow syntax for anonymous functions - JavaScript Tutorial

From the course: JavaScript: Best Practices for Functions and Classes

Start my 1-month free trial

Use arrow syntax for anonymous functions

- [Instructor] Named functions are a very common case when you need to create functions. Another is creating callback functions, like here in the start file for this video. This callback is created with a function keyword, which adds to the line length and can make the code harder to read. To avoid this, it's common to prefer arrow functions for inline callbacks. ESLint supports the prefer arrow callback rule, which lets you specify that ESLint should throw an error if your code doesn't use an arrow function for a callback. I'll add this to my eslintrc file. So, prefer-arrow-callback with error as the value, and I'll save that. And over in my JS file, I have an error and that is an unexpected function expression and it names the eslint rule that I just added. So, to fix this, I want to rewrite my callback function as an arrow function. And that's actually pretty straightforward to do. First, I'll take out that function…

Contents