JavaScript
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax and Types
-
Section 3: Strings and Numbers in Depth
-
Section 4: Control Flow
-
Section 5: Functions
-
Section 6: Objects and Arrays
-
Section 7: Maps, Sets, and Symbols
-
Section 8: Asynchronous JavaScript
-
Section 9: Object-Oriented and Prototypes
-
Section 10: The DOM
-
Section 11: Browser APIs
-
Section 12: Modern JavaScript (ES2015-ES2025)
-
Section 13: Functional Programming Patterns
-
Section 14: Error Handling and Debugging
-
Section 15: Testing
-
Section 16: Accessibility for JavaScript Developers
-
Section 17: Internationalization and Localization
-
Section 18: Performance
-
Section 19: Node.js Fundamentals
-
Section 20: Regular Expressions
-
Section 21: Design Patterns in JavaScript
-
Section 22: Security Basics
-
Section 23: Data Structures and Algorithms in JavaScript
-
Section 24: Practical Projects
-
Section 25: More Advanced Async Patterns
-
Section 26: More Object and Class Practice
-
Section 27: Working with Dates and Internationalization
-
Section 28: Web Components
-
Section 29: More DOM and Browser Practice
-
Section 30: Build Tooling for Vanilla JavaScript
-
Section 31: More Practice Projects
-
Section 32: Interview and Algorithm Practice
-
Section 33: Error Objects (MDN Reference)
-
Section 34: TypedArrays and Binary Data
-
Section 35: Reflection and Metaprogramming (MDN Reference)
-
Section 36: More Global Functions (MDN Reference)
29: Arrow Functions
You've seen functions written the traditional way, but if you look at any modern codebase, you'll see => everywhere. These are arrow functions. They aren't just a "shorthand" to save a few keystrokes; they actually behave differently in a couple of key ways that will save you from some massive headaches later on.
To show you how they work, let's build a simple price calculator for a store that applies a percentage discount to a list of items.
Starting with a standard function
I'll start by writing a basic function to calculate a discounted price. I'm using a standard function expression here just so we have a baseline to compare against.
const calculateDiscount = function(price, discount) {
return price - (price * discount);
};
console.log(calculateDiscount(100, 0.2)); // 80
It works, but it's a bit wordy. We have the function keyword, the curly braces, and the explicit return statement. For a one-liner like this, it feels like a lot of overhead.
Cleaning up the syntax
Now, let's convert this to an arrow function. I'll strip out the function keyword and place the "fat arrow" after the parameters.
const calculateDiscount = (price, discount) => {
return price - (price * discount);
};
Notice that the logic hasn't changed, but the visual noise is gone. I personally find this much easier to scan when I'm reading through a large file.
Going full minimalist with implicit returns
Here is where arrow functions get really powerful. Since my function only does one thing—returns a value—I can remove the curly braces and the return keyword entirely. This is called an implicit return.
const calculateDiscount = (price, discount) => price - (price * discount);
That's it. One line. If you have a function that just transforms data and hands it back, this is the way to go. Just remember: the moment you add those curly braces back in, the implicit return disappears, and you must use the return keyword again, or your function will return undefined.
Hitting the 'this' wall
I want to put this calculator inside a "Store" object to keep things organized. I'll try to use an arrow function for a method that calculates the total for a cart. Watch what happens here—I'm making a common mistake.
const store = {
taxRate: 0.07,
calculateTotal: (price) => {
return price + (price * this.taxRate);
}
};
console.log(store.calculateTotal(100)); // NaN
Wait, why did that return NaN? Because this.taxRate is undefined. Here is the deal: arrow functions do not have their own this context. They inherit this from the surrounding scope (in this case, the global window or module), not from the object they are defined in.
If you need a function to act as an object method and access properties of that object, you have to go back to the traditional function syntax. Let me fix that:
const store = {
taxRate: 0.07,
calculateTotal(price) {
return price + (price * this.taxRate);
}
};
console.log(store.calculateTotal(100)); // 107
So, the rule of thumb I use is: use arrow functions for almost everything—especially callbacks and data transformation—but stick to regular functions for object methods.
📋 Practical Task
Build a Temperature Converter Utility
Create a small utility object called tempConverter. It should contain the following:
- A property
offset: 32. - A method
toFahrenheit(celsius)(written as a regular function) that adds theoffsetto the celsius value. - A standalone arrow function called
celsiusToKelvinthat takes a celsius value and implicitly returns it plus 273.15.
Test both functions by logging the results to the console.
There are no comments for now.