DEV Community

felixd38v_
felixd38v_

Posted on

JavaScript | What Are Arrow Functions?

Arrow functions are a simpler way to write functions in JavaScript. They're shorter and easier to read.

Example 1:

Here's an example that uses a traditional function to square a number:

function square(num) {
    return num * num;
}
Enter fullscreen mode Exit fullscreen mode

And here's the same functionality using an arrow function:

const square = (num) => num * num;
Enter fullscreen mode Exit fullscreen mode

Example 2:

Here's an example that uses a traditional function to find the average of three numbers:

function average(a, b, c) {
    return (a + b + c) / 3;
}
Enter fullscreen mode Exit fullscreen mode

And here's the same functionality using an arrow function:

const average = (a, b, c) => (a + b + c) / 3;
Enter fullscreen mode Exit fullscreen mode

As you can see, arrow functions can make your code look cleaner.

References:

JavaScript Documentation

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay