# JavaScript: Using Arrow Functions for Cleaner Syntax
Arrow functions in JavaScript provide a more concise syntax for writing function expressions, making your code easier to read and maintain.
Consider an example where you want to create a function that takes two numbers and returns their sum. Using the traditional function expression:
// Traditional function expression
const add = function (a, b) {
return a + b;
};
With arrow functions, you can simplify the syntax and make the function more readable:
// Arrow function
const add = (a, b) => a + b;
Arrow functions not only shorten the syntax but also have other benefits, such
as automatic binding of the this keyword to the surrounding scope. Here’s an
example of using an arrow function with the map() method. As you can see,
arrow functions make your code more concise and easier to read.
const numbers = [1, 2, 3, 4, 5];
// Traditional function expression
const squares1 = numbers.map(function (number) {
return number * number;
});
// Arrow function
const squares2 = numbers.map((number) => number * number);