Arrow function vs function — the difference between scope

Serhii Koziy
3 min readFeb 19, 2023

--

JavaScript is a powerful and versatile programming language used for web development. One of the most powerful features of JavaScript is the ability to create functions. Two common ways to create functions in JavaScript are the traditional function syntax and the newer arrow function syntax. In this article, we will explore the difference between arrow functions and traditional functions in terms of scope.

Traditional Function Syntax

In JavaScript, traditional functions are created using the function keyword, followed by the function name and a set of parentheses. The body of the function is enclosed in curly braces, and any parameters are passed in the parentheses. Here’s an example of a traditional function that takes two parameters and returns their sum:

function addNumbers(a, b) {
return a + b;
}

The scope of a traditional function is determined by where it is defined in the code. If a variable is defined inside a traditional function, it is only accessible within that function, and not outside of it. Similarly, if a variable is defined outside of a function, it is accessible within the function as long as it is not overridden by a variable with the same name defined within the function.

Arrow Function Syntax

Arrow functions are a newer syntax for creating functions in JavaScript. They are shorter and more concise than traditional functions, and they also have some differences in how they handle scope. Here’s an example of an arrow function that takes two parameters and returns their sum:

const addNumbers = (a, b) => a + b;

In the arrow function syntax, the parameters are listed in parentheses, followed by an arrow (=>) and the function body. If the function body is a single expression, it can be written without curly braces. If the function body has multiple statements, it must be enclosed in curly braces.

Scope Differences Between Arrow Functions and Traditional Functions

One of the main differences between arrow functions and traditional functions is how they handle scope. Arrow functions do not have their own this value, whereas traditional functions do. This means that the value of this inside an arrow function is the same as the value of this outside the function, while in a traditional function, this can be different depending on how the function is called.

Another difference in scope between arrow functions and traditional functions is how they handle the arguments object. In a traditional function, the arguments object is automatically created and contains all of the arguments passed to the function. In an arrow function, there is no arguments object, so if you need to access the arguments passed to the function, you must use the rest parameter syntax. For example:

// Traditional function
function sum() {
let total = 0;
for (let i = 0; i < arguments.length; i++) {
total += arguments[i];
}
return total;
}
// Arrow function
const sum = (...args) => {
let total = 0;
for (let i = 0; i < args.length; i++) {
total += args[i];
}
return total;
};

Finally, the way arrow functions handle the this keyword can also affect scope. Since arrow functions do not have their own this value, they can be useful in situations where you need to preserve the value of this from the outer scope. For example, consider the following code:

const person = {
name: 'John',
age: 30,
sayHello: function() {
console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
}
};

In this example, the sayHello function uses this to refer to the person object. However, if we were to use an arrow function instead, the value of this would be the same as it is outside the function

--

--

No responses yet