Skip to main content

Command Palette

Search for a command to run...

Arrow Functions vs Regular Functions: What You Need to Know

Updated
3 min read
Arrow Functions vs Regular Functions: What You Need to Know
H

👨‍💻 Full Stack Developer | MERN & Beyond | Generative AI Enthusiast As a Full Stack Developer, I specialize in building modern, scalable web applications using the MERN stack and beyond. During my previous internship roles, I contributed to both frontend and backend development on real-world projects, working with technologies like React, Node.js, and microservices-based architectures. With a BTech in Computer Science, I’ve built a strong foundation in programming and software development. I'm passionate about continuous learning and personal growth — and I document my journey as I explore new technologies, sharpen my skills, and strive to become a better engineer every day.

Functions are code blocks that perform a certain task. They help us break code into smaller pieces, reuse logic across different parts of our program, and make our code easier to read and maintain.

With the introduction of arrow functions in ES6, JavaScript developers got a shorter and often cleaner way to write functions — but arrow functions also come with some important differences from traditional functions. Let’s explore.

Why Functions Matter

Before we dive into arrow functions, let’s quickly recap why functions are important in the first place.

  1. Modularity – Functions break complex tasks into smaller, manageable units.

  2. Reusability – You write code once and reuse it wherever you need it.

  3. Maintainability – Smaller, well-defined functions make code easier to debug and update.

// Example Function to add two numbers
function add(a, b) {
  return a + b;
}
console.log(add(3, 2)); // output: 5

Instead of repeating the add calculations, we can simply call the add function whenever we want without rewriting the logic.

Two Terminologies Regarding Functions

  • Parameters - When we pass a variable during the function declaration, it is known as a parameter. In the above example, when we are declaring the add(a, b) function, here a and b are called parameters.

  • Arguments - When we pass the value of the variables while calling a function, it is called arguments. In the above, when we are calling the add(3, 2) function, here 3 and 2 are called arguments.

Types of Functions in JavaScript

JavaScript offers multiple ways to write functions. Let’s compare them.

  1. Function Declaration - When we begin the function declaration with the function keyword, followed by the function name and some parameters, and the function body enclosed in curly braces. Function declarations are hoisted, which means you can call them before they’re defined.

     function greet(name) {
       return `Hello, ${name}!`;
     }
     console.log(greet("Hardik"));
    
  2. Function Expression - When we begin the function declaration with the function keyword and the whole function is assigned to a variable, it is known as a function expression. The function can be named or anonymous. Function expressions are not hoisted.

     const greet = function(name) { 
       return `Hello, ${name}!`;
     };
     console.log(greet("Hardik"));
    
  3. Arrow Function (ES6) - Arrow functions were introduced in ES6 and are part of modern JavaScript. In arrow, simply refers to the \=>, and you don't need the function keyword, giving us a shorter syntax for function expressions.

     const greet = (name) => `Hello, ${name}!`;
     console.log(greet("Hardik"));
    
     const greet = (name) => {
     return `Hello, ${name}!`;
     ;
     console.log(greet("Hardik"));
    

    Conclusion

    Arrow functions make JavaScript code shorter and often easier to read — especially for callbacks and inline logic. However, they behave differently from regular functions, particularly with this and hoisting, so choosing the right function type depends on your specific use case.