Building Your Learning Module...
Getting things ready for you!
Find videos you like?
Save to resource drawer for future reference!
Arrow functions are a shorter way to write functions in modern JavaScript. They use => instead of the word "function"!
=> symbol is called a "fat arrow" - it points to what the function returns!function greet(name) {
return 'Hello, ' + name;
}
console.log(greet('Alice'));
// Output: Hello, Aliceconst greet = (name) => {
return 'Hello, ' + name;
}
console.log(greet('Alice'));
// Output: Hello, Alicefunction (name) { ... }(name) => { ... }const greet = (name) => { ... }Different ways to write arrow functions
If your function only returns one expression, you can skip the curly braces { } and the return keyword!
Long version:
const double = (n) => { return n * 2; }Short version:
const double = (n) => n * 2One-liners without curly braces
One or two lines of code
Calculations, transformations
Array methods, setTimeout
Both ways work fine!
Pick one style in your project
Use regular functions first, then try arrows
Arrow functions are great with array methods
(params) => expression
No "function" keyword needed
Skip { } for one-line functions
Popular in modern code