JavaScript Arrow Functions Tutorial
JavaScript arrow function tutorial
Learn how to use modern, concise arrow functions in JavaScript.
Arrow functions are a concise way to write functions in JavaScript, introduced in ES6 (ECMAScript 2015). They make your code cleaner and are especially useful when working with callbacks, array methods, and concise function expressions.
An arrow function is a shorter syntax for writing function expressions.
// Traditional function
function add(a, b) {
return a + b;
}
// Arrow function
const add = (a, b) => a + b;
Arrow functions are best used for:
Callback functions (e.g., in map
, filter
, forEach
)
Short, single-purpose functions
Lexical this
binding (no new this
context)
No this
binding – Arrow functions inherit this
from the parent scope.
Cannot be used as constructors – You can't use new
with arrow functions.
No arguments
object – Use rest parameters instead if needed.
const square = n => n * n;
const getUser = () => ({ name: "John", age: 30 });
const nums = [1, 2, 3];
const doubled = nums.map(num => num * 2);
Avoid arrow functions when:
You need dynamic this
(e.g., inside class methods)
You need to use arguments
Arrow functions are a powerful tool in modern JavaScript, helping you write cleaner and more readable code. Understanding when and how to use them effectively is key to becoming a better JavaScript developer.