DEV Community

Cover image for Understanding Normal Functions and Arrow Functions in JavaScript
Dhruv Kumar
Dhruv Kumar

Posted on

Understanding Normal Functions and Arrow Functions in JavaScript

JavaScript, as a core technology of web development, offers a versatile syntax for writing functions. Among these, normal (or traditional) functions and arrow functions stand out for their distinct characteristics and uses. Let's dive into the differences between these two types of functions, exploring their syntax, behavior, and the contexts in which they are most effectively used.
Syntax
The most noticeable difference between normal functions and arrow functions is their

  • Normal Function:
function greet(name) {
  return `Hello, ${name}!`;
}
Enter fullscreen mode Exit fullscreen mode
  • Arrow Function:
const greet = name => `Hello, ${name}!`;
Enter fullscreen mode Exit fullscreen mode

this Keyword Behavior
One of the most significant differences between normal functions and arrow functions is how they handle the this keyword.

  • Normal Function : In normal functions, the value of this is determined by how the function is called. It can vary based on the calling context or be explicitly set using bind, call, or apply.
const person = {
  name: 'Alice',
  greet: function() {
    console.log(`Hello, ${this.name}`);
  }
};

person.greet(); // Logs: "Hello, Alice"
Enter fullscreen mode Exit fullscreen mode
  • Arrow Function : Arrow functions do not have their own this context; instead, they inherit this from the surrounding lexical scope. This makes them particularly useful for callbacks and methods that need to refer to the context in which they were defined.
const person = {
  name: 'Alice',
  greet: () => {
    console.log(`Hello, ${this.name}`);
  }
};

person.greet(); // Logs: "Hello, undefined" in the global scope
Enter fullscreen mode Exit fullscreen mode

Constructor Use
Another difference is related to their use as constructors.

  • Normal Function : Normal functions can be used as constructors with the new keyword. They can have properties and methods assigned to their prototype.
function Person(name) {
  this.name = name;
}

const alice = new Person('Alice');
console.log(alice.name); // Logs: "Alice"
Enter fullscreen mode Exit fullscreen mode
  • Arrow Function : Arrow functions cannot be used as constructors and will throw an error if used with new.
const Person = (name) => {
  this.name = name;
};

// Throws an error: TypeError: Person is not a constructor
const alice = new Person('Alice');
Enter fullscreen mode Exit fullscreen mode

the arguments Object

  • Normal functions : JavaScript have access to an arguments object, which is an array-like object. This object contains all the arguments passed to the function, allowing you to access and use these arguments even if they weren't explicitly defined as parameters in your function declaration.
function sum() {
  let total = 0;
  for(let i = 0; i < arguments.length; i++) {
    total += arguments[i];
  }
  return total;
}

console.log(sum(1, 2, 3, 4)); // Output: 10
Enter fullscreen mode Exit fullscreen mode

In this example, the sum function doesn't define any parameters, but it uses the arguments object to access all arguments passed to it, sum them, and return the result.

  • Arrow Function : Arrow functions, introduced in ES6, do not have their own arguments object. If you try to access the arguments object inside an arrow function, you'll end up accessing the arguments object from the closest normal function scope (or the global scope, if no normal function is found), which is often not what you want. To achieve similar functionality in arrow functions, you can use rest parameters. Rest parameters allow you to represent an indefinite number of arguments as an array. Here's how you can use rest parameters with an arrow function:
const sum = (...args) => {
  let total = 0;
  for(let i = 0; i < args.length; i++) {
    total += args[i];
  }
  return total;
}

console.log(sum(1, 2, 3, 4)); // Output: 10

Enter fullscreen mode Exit fullscreen mode

In this example, the sum arrow function uses the rest parameters syntax (...args) to collect all passed arguments into an array named args. The function then iterates over this array to calculate the sum of its elements.

Developer Tip
Arrow functions cannot be accessed before initialization even if you initialize with var keyword 😉

Conclusion
Choosing between normal functions and arrow functions in JavaScript depends largely on the specific requirements of the code you're writing. Arrow functions, with their concise syntax and lexical this binding, offer a modern and efficient way to write functions where the context does not need to change. Normal functions, on the other hand, provide more flexibility in terms of this binding and their use as constructors, making them suitable for a broader range of programming patterns. Understanding the differences between these two function types is crucial for writing effective and clean JavaScript code.

Top comments (0)