# Tweet by Sukka / 毛绒绒的大尾巴🦊 (@isukkaw)
The arrow function is just an easy-to-write syntactic sugar of function declaration without "this", and also produces a smaller output after modification (mangle).
If it has no context, no edge cases, and is not a challenge, why should not people prefer the arrow function?
> **Manuel Spigolon** @ManuEomm · Sep 13
>
> When did we stop writing named functions in favor of variable declarations with long arrow functions assigned? I'm seeing this trend over and over again on StackOverflow 😔 If you need a function... declare a function!
11:14 PM · Sep 14, 2024 · 285 Views ```
I am not familiar with Javascript. I was wondering if you could:
1. Give me a brief introduction on what the tweet above is talking about.
2. Explain the core concepts that I need to understand the tweet above with simplest code examples.
Assistant
Part 1: Introduction to the Tweet Discussion
The tweet you've provided captures a conversation between two developers, Manuel Spigolon and Sukka (@isukkaw), about the use of arrow functions versus traditional function declarations in JavaScript.
Manuel Spigolon is expressing concern over a trend he's noticed on platforms like StackOverflow, where developers are increasingly using variable declarations assigned to arrow functions instead of named function declarations. He suggests that if someone needs a function, they should declare it using the traditional function syntax for clarity and readability.
Sukka responds by advocating for the use of arrow functions. He argues that arrow functions are:
Easier to write (syntactic sugar) compared to traditional functions.
Do not have their own this context, which can simplify certain scenarios.
Result in smaller code outputs after code minification (also known as mangling), which is beneficial for performance.
Sukka is essentially questioning why developers shouldn't prefer arrow functions when there are no specific context issues, edge cases, or challenges that necessitate the use of traditional functions.
Part 2: Core Concepts Explained with Simple Code Examples
To understand the discussion, let's delve into the core concepts:
Traditional Function Declarations
Function Expressions
Arrow Functions
The this Context in JavaScript
Code Minification and Mangling
1. Traditional Function Declarations
A traditional function declaration uses the function keyword and has the following syntax:
javascript
function functionName(parameters) {
// function body
}
In the arrow function, this.name doesn't refer to person.name because this is inherited from the outer scope, which is likely the global scope where name may not be defined.
Why Does this Behave Differently?
Traditional Functions: The value of this is determined by how the function is called.
Arrow Functions: The value of this is determined by the surrounding scope (lexical scope) at the time the arrow function is defined.
Use Case Example:
When you need to preserve the context of this inside a function, arrow functions can be helpful.
The arrow function inside setInterval doesn't have its own this, so it uses the this from Counter, correctly referring to the instance's count property.
5. Code Minification and Mangling
Code Minification is the process of removing unnecessary characters (like whitespace, comments) from the source code without changing its functionality, to reduce file size.
Mangling goes a step further by renaming identifiers (like variables and function names) to shorter names.
Why Does This Matter?
Arrow Functions Are Shorter: They use fewer characters than traditional function declarations.
Smaller Code After Mangling: Minifiers can further reduce the code size with arrow functions because they often involve fewer characters and can be more aggressively optimized.
Comparison Example:
Traditional Function Declaration:
javascript
function sum(a, b) {
return a + b;
}
Arrow Function:
javascript
const sum = (a, b) => a + b;
The arrow function uses fewer characters and can result in a smaller minified file.
Conclusion:
In the context of the tweets:
Manuel is advocating for the clarity and maintainability of traditional named functions.
Sukka is highlighting the convenience and performance benefits of arrow functions, especially when the function doesn't need its own this context.
When to Use Each:
Use Traditional Functions When:
You need a named function (helpful for recursion or debugging).
You need to control the this context.
You want to leverage function hoisting.
Use Arrow Functions When:
You prefer concise syntax.
You don't need a separate this context.
You're writing simple one-liner functions or callbacks.
Summary with Examples:
Traditional Function Declaration:
javascript
function multiply(a, b) {
return a * b;
}
console.log(multiply(3, 4)); // Output: 12
Function Expression:
javascript
const divide = function(a, b) {
return a / b;
};
console.log(divide(10, 2)); // Output: 5
Arrow Function:
javascript
const add = (a, b) => a + b;
console.log(add(5, 7)); // Output: 12
Arrow functions provide a concise syntax and inherit this from the enclosing scope.
Traditional functions have their own this and can be named, aiding in clarity and debugging.
Choosing between them depends on the specific needs of your code, considering factors like this binding, readability, and performance.
I hope this explanation clarifies the discussion in the tweet and helps you understand the core concepts of arrow functions and traditional functions in JavaScript!