Hello! Welcome to your first lesson in the "Introduction to React" course.
Based on our conversation, we're starting with a foundational module on modern JavaScript to make sure you have a solid footing before we dive into React itself. This lesson kicks off that module.
Today, we will focus on arrow functions. By the end of this lesson, you will be able to use the concise syntax of arrow functions and, more importantly, understand how they handle the this keyword differently from traditional functions—a concept known as lexical this binding. This is a cornerstone of modern JavaScript and is used extensively in React.
Let's begin.
1. From Traditional Functions to Arrow Functions
You're already familiar with writing functions in JavaScript using the function keyword. ES6 introduced a more concise way to write functions, called arrow functions. They are not just a shorter syntax; they also have a very important behavioral difference that we'll cover shortly.
First, let's get comfortable with the syntax.

The basic idea is to replace the function keyword with a "fat arrow" => after the parameters.
To see this in action, the following video provides a clear, step-by-step conversion of various traditional functions into their arrow function equivalents.
JavaScript ES6 Arrow Functions Tutorial
Watch this section from 'JavaScript ES6 Arrow Functions Tutorial' by Web Dev Simplified. It demonstrates how to convert functions with different numbers of parameters into the new arrow function syntax.
Watch from the beginning until timestamp 05:28. Pay attention to the syntax for functions with multiple parameters, a single parameter (where you can omit the parentheses), and no parameters. Also, notice the 'implicit return' feature for one-line functions.
Key Syntax Rules:
- Basic Conversion:
function(a, b) { ... }becomes(a, b) => { ... }. - Single Parameter:
function(name) { ... }can be simplified toname => { ... }. The parentheses around the parameter are optional. - No Parameters:
function() { ... }becomes() => { ... }. The parentheses are required. - Implicit Return: If the function body is a single expression, you can omit the curly braces
{}and thereturnkeyword. The result of the expression is returned automatically.(a, b) => { return a + b; }simplifies to(a, b) => a + b;
Practice: Your Turn to Convert
Let's solidify this. Try rewriting the following traditional functions as arrow functions, making them as concise as possible.
Function 1: A simple greeting
function getGreeting(name) {
return 'Hello, ' + name + '!';
}
Function 2: An event listener callback
document.addEventListener('click', function() {
console.log('Document was clicked!');
});
Function 3: An array transformation using .map()
const numbers = [1, 2, 3, 4];
const squaredNumbers = numbers.map(function(num) {
return num * num;
});
Click to see the solutions
Solution 1:
const getGreeting = name => `Hello, ${name}!`;
// We use a template literal for cleaner string concatenation.
Solution 2:
document.addEventListener('click', () => {
console.log('Document was clicked!');
});
Solution 3:
const numbers = [1, 2, 3, 4];
const squaredNumbers = numbers.map(num => num * num);
As you can see, especially with callbacks for array methods, the syntax becomes much cleaner and easier to read.
2. The Most Important Difference: Lexical this
Beyond the shorter syntax, the primary reason arrow functions are so transformative is how they handle the this keyword. This is the most critical concept to grasp in this lesson.
- A regular function defines its own
thisvalue based on how it is called (e.g., the object that invoked it, or the globalwindowobject). This dynamic nature ofthiscan be a common source of bugs. - An arrow function does not have its own
this. Instead, it inheritsthisfrom its parent scope at the time it is defined. This is called lexical scoping.
Let's explore this through a text-based example first. The following article provides an excellent explanation with a very common scenario: using this inside a .forEach() loop.
Understanding Arrow Functions in JavaScript
Please read the 'Lexical this' section from the article 'Understanding Arrow Functions in JavaScript' on DigitalOcean.
In the article, find the section titled 'Lexical this'. Read through it to understand why this.phrase is undefined in the traditional function example and how an arrow function solves this problem without needing .bind(this).
Now, let's reinforce this with a video explanation that uses another classic example involving setTimeout.
JavaScript ES6 Arrow Functions Tutorial
This next clip from the 'Web Dev Simplified' video visually demonstrates the problem with this in regular functions and how arrow functions fix it.
Watch from 05:28 to 09:00. The video uses a Person class to show how this.name is lost inside a setTimeout with a regular function but is correctly accessed with an arrow function.
Putting It All Together
Let's break down the behavior with a clear code example. Imagine you have an object with a method that logs a message after a delay.
const myComponent = {
id: 'Component-123',
// Using a regular function for the callback
logIdRegular: function() {
console.log('Outer function this.id:', this.id); // Correctly logs 'Component-123'
setTimeout(function() {
// The `this` inside this callback is NOT the myComponent object.
// In a browser, it's the `window` object.
console.log('Regular function callback this.id:', this.id); // Logs 'undefined'
}, 500);
},
// Using an arrow function for the callback
logIdArrow: function() {
console.log('Outer function this.id:', this.id); // Correctly logs 'Component-123'
setTimeout(() => {
// The arrow function inherits `this` from its parent scope (logIdArrow).
// So, `this` is still the myComponent object.
console.log('Arrow function callback this.id:', this.id); // Correctly logs 'Component-123'
}, 1000);
}
};
myComponent.logIdRegular();
myComponent.logIdArrow();
When you run this, you'll see that the arrow function "remembers" what this was in its surrounding context. This predictable behavior is invaluable in React, especially when dealing with event handlers that need to update a component's state.
Conclusion
In this lesson, we've taken our first step in our JavaScript refresher. You've learned the two key aspects of arrow functions that make them essential for modern development.
Key Takeaways:
- Concise Syntax: Arrow functions (
=>) provide a shorter way to write functions, which is especially useful for callbacks in methods like.map()and.filter(). - Lexical
this: This is the most important feature. Arrow functions do not create their ownthiscontext. They inheritthisfrom their parent scope, which makes your code more predictable and avoids common bugs associated with thethiskeyword in asynchronous code or nested functions.
In React, you will use arrow functions constantly—for defining components, handling events, and working with data. Their predictable this binding eliminates an entire category of problems that developers used to face.
Next Up:
In the next lesson, we'll cover another powerful ES6 feature: destructuring. You'll learn how to elegantly extract values from arrays and objects, a skill you will use every day when working with props and state in React.