Arrow Functions in JavaScript: A Simpler Way to Write Functions

Search for a command to run...

No comments yet. Be the first to comment.
When writing JavaScript code, you often need to perform calculations, compare values, or combine conditions. To do these tasks, JavaScript uses something called operators. If you’ve ever done basic ma

When learning JavaScript, you’ll eventually encounter a keyword that can feel confusing at first: this. The value of this changes depending on how a function is called. JavaScript also provides specia

Functions are one of the most important concepts in JavaScript. They help you organize your code and avoid repeating the same logic again and again. In JavaScript, there are multiple ways to create fu

When writing JavaScript programs, you often need to store multiple values together. For example, imagine you want to store a list of fruits: Apple Mango Banana Orange You could store them like t

As JavaScript applications grow, managing code and data can become complicated. To make programs easier to organize and reuse, developers often use a programming style called Object-Oriented Programmi

ygshjm
24 posts
if you’ve been writing javascript for a while, you’re used to the function keyword. it’s like a classic formal letter—it gets the job done, but it’s a bit wordy and feels a bit old-school.
arrow functions (introduced in ES6) are like sending a quick text message. they are shorter, cleaner, and honestly, they just look more modern. they were built to reduce "boilerplate"—that extra fluff code we used to write over and over again.
open your browser console (f12) and let's see why everyone is switching to the arrow.
imagine you’re at a cafe. instead of saying, "excuse me, waiter, could i please have one cup of black coffee,"
you just say, "one black coffee, please.
" the "problem" with old functions was that they were too wordy.
every time you wanted to do something tiny, you had to write the word function, then {}, then return.
arrow functions were created to cut the noise and let you focus on the logic.
the "arrow" (=>) basically replaces the function keyword. it’s like a shortcut that tells javascript: "here is an input, and here is what i want you to do with it."
the old way (normal function):
function sayHi() {
return "hey, what's up?";
}
the modern way (arrow function):
const sayHi = () => {
return "hey, what's up?";
};
arrow functions get even cooler when you start passing data into them.
if you only have one input, you don't even need the parentheses ().
// old way
function square(n) {
return n * n;
}
// arrow way (look how clean this is!)
const square = n => n * n;
multiple parameters: keep the brackets
if you have two or more inputs, you keep the parentheses so the computer doesn't get confused about where the inputs start.
const add = (a, b) => a + b;
console.log(add(5, 10)); // result: 15
this is where arrow functions really shine.
explicit return: you manually write the word return inside curly braces {}.
implicit return: if your code is just one line, you can delete the {} and the word return.
it’s "implied"—javascript just knows you want to return that value. it’s like your best friend knowing exactly what you want to eat without you having to say the whole order.
// explicit (formal)
const double = (n) => {
return n * 2;
};
// implicit (modern/short)
const double = n => n * n;
besides looking cooler, there are a few things to keep in mind:
no function keyword: you save space and time.
readability: when you use arrow functions inside things like map or filter, your code starts reading like a sentence.
this keyword: normal functions and arrow functions handle the word this differently. for now, just think of arrow functions as being more "loyal"—they keep the context of where they were born, which makes them great for things like timers or buttons.
the best way to learn is to type it out. don't just copy-paste; try to write these from memory in your console:
the greeting: create an arrow function that takes a name and returns "hello [name]".
the math: create an arrow function that takes two numbers and returns their product (multiplication). use the implicit return (no {} or return keyword!).
the array challenge:
create an array: let nums = [2, 5, 10, 15];
use map() with an arrow function to double every number.
use filter() with an arrow function to only keep numbers greater than 10.