JavaScript Arrow Functions vs JavaScript Functions
- English 🇺🇸
- 한국어 🇰🇷
When to Use Each
Use traditional functions
- When you need to use function hoisting (arrow functions are not hoisted).
- If you need to access the
arguments
object. - When using methods that will be added to an object's prototype.
- If you need a dynamic context (
this
), such as in event handlers.
Use arrow functions
- For shorter syntax in functional programming scenarios.
- When working with higher-order functions that expect a function callback.
- In cases where you want to retain the lexical
this
scope (e.g., in nested functions or event handlers). - For single-line functions with implicit returns.
Differences
Syntax
Arrow functions provide a shorter syntax. They are written with an arrow rather than the function
keyword.
Traditional function
function add(a, b) {
return a + b
}
Arrow function:
const add = (a, b) => a + b;
this
Binding
One of the most significant differences is how this
is handled. In traditional functions, this
is dynamic and can change depending on the context in which the function is called. In arrow functions, this
is lexically scoped, meaning it uses this
from the surrounding code where the function is defined.
No Binding of arguments
Arrow functions do not have their own arguments
object. Instead, they access the arguments
object of the closest non-arrow parent function.
Cannot be used as Constructors
Arrow functions cannot be used as constructor functions. They cannot be called with new
.
No Duplicate Named Parameters
Traditional functions allow duplicate named parameters in non-strict mode, while arrow functions do not, regardless of strict mode.
No prototype
Property
Arrow functions do not have a prototype
property.
Implicit Return
In arrow functions, if you have a single expression, you can omit the curly braces {}
and the return
statement. The expression automatically returns its result.
각각 사용할 때
전통적인 함수 사용
- 함수 호이스팅을 사용해야 할 때 (화살표 함수는 호이스팅되지 않는다).
arguments
객체에 접근해야 할 때.- 객체의 프로토타입에 추가될 메소드를 사용할 때.
- 동적 맥락(
this
)이 필요할 때, 예를 들어 이벤트 핸들러에서.
화살표 함수 사용
- 함수형 프로그래밍 상황에서 더 짧은 문법이 필요할 때.
- 함수 콜백을 기대하는 고차 함수와 함께 작업할 때.
- 어휘적
this
범위를 유지하고자 할 때 (예: 중첩 함수나 이벤트 핸들러에서). - 암시적 반환을 가진 단일 줄 함수에 대해.
차이점
문법
화살표 함수는 더 짧은 문법을 제공한다. function
키워드 대신 화살표로 작성된다.
전통적인 함수
function add(a, b) {
return a + b
}
화살표 함수:
const add = (a, b) => a + b;