9609c04755
W3Schools 튜토리얼을 P-Reinforce v3.1 포맷으로 위키화(영어 본문, 한/영 섹션 헤더). - Topic_HTML: 59문서 (튜토리얼+예제, 레퍼런스/메타 제외) - Topic_CSS: 190문서 (메인 + Advanced/Flexbox/Grid/RWD 전체) - Topic_JavaScript: 120문서 (코어 언어; Temporal/DOM상세/BOM/WebAPI/AJAX/jQuery/Graphics 등은 후속) 각 폴더 00_INDEX.md(MOC) 포함. 코드 verbatim, 미확인분은 "Not found in source" 표기. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
204 lines
7.8 KiB
Markdown
204 lines
7.8 KiB
Markdown
---
|
|
id: javascript-arrow-functions
|
|
title: "JavaScript Arrow Functions"
|
|
category: "Frontend"
|
|
status: "draft"
|
|
verification_status: "conceptual"
|
|
canonical_id: ""
|
|
aliases: ["JS arrow functions", "arrow function syntax", "fat arrow", "=> functions", "lexical this", "concise functions"]
|
|
duplicate_of: ""
|
|
source_trust_level: "B"
|
|
confidence_score: 0.88
|
|
created_at: 2026-06-23
|
|
updated_at: 2026-06-23
|
|
review_reason: ""
|
|
merge_history: []
|
|
tags: ["javascript", "js", "web", "frontend", "w3schools", "functions", "arrow-functions", "this"]
|
|
raw_sources: ["https://www.w3schools.com/js/js_arrow_function.asp"]
|
|
applied_in: []
|
|
github_commit: ""
|
|
---
|
|
|
|
# [[JavaScript Arrow Functions]]
|
|
|
|
## 🎯 한 줄 통찰 (One-line insight)
|
|
Arrow functions give a shorter syntax for function expressions and do not have their own `this` — they inherit it from the surrounding code, which makes them great for callbacks but poor as object methods. [S1]
|
|
|
|
## 🧠 핵심 개념 (Core concepts)
|
|
- **Shorter function expressions** — arrow functions let you skip the `function` keyword, the `return` keyword, and the curly brackets. [S1]
|
|
- **Always an expression** — an arrow function uses `=>` and is always written as a function expression. [S1]
|
|
- **Implicit return** — a single-statement body returns its value automatically without `return` or braces. [S1]
|
|
- **Parentheses rules** — one parameter can omit parentheses; zero or multiple parameters require them. [S1]
|
|
- **No own `this`** — arrow functions do not have their own `this`; they inherit it from the surrounding code, so they are unsuitable as object methods. [S1]
|
|
- **Not hoisted** — arrow functions are expressions, must be assigned to a variable, and cannot be used before they are defined. [S1]
|
|
|
|
## 🧩 추출된 패턴 (Extracted patterns)
|
|
- **Concise one-liner** — `const multiply = (a, b) => a * b;` collapses an expression into a single statement with implicit return. [S1]
|
|
- **Single-param shorthand** — drop the parentheses for exactly one parameter: `const square = x => x * x;`. [S1]
|
|
- **Always-keep-braces habit** — because implicit return only works for a single statement, keeping `{ return ... }` avoids accidental `undefined`. [S1]
|
|
|
|
## 📖 세부 내용 (Details)
|
|
Arrow functions allow a shorter syntax for function expressions. You can skip the `function` keyword, the `return` keyword, and the curly brackets. [S1]
|
|
|
|
**Arrow Function Syntax**
|
|
An arrow function uses the `=>` symbol and is always written as a function expression. [S1]
|
|
```javascript
|
|
const add = (a, b) => {
|
|
return a + b;
|
|
};
|
|
```
|
|
|
|
**Shorter Syntax**
|
|
If the function body contains only one statement, you can remove the word `function`, the curly brackets, and the `return` keyword. Before arrow: [S1]
|
|
```javascript
|
|
const multiply = function(a, b) {return a * b}
|
|
```
|
|
With arrow: [S1]
|
|
```javascript
|
|
const multiply = (a, b) => a * b;
|
|
```
|
|
With arrow: [S1]
|
|
```javascript
|
|
const hello = () => "Hello World!";
|
|
```
|
|
Before arrow: [S1]
|
|
```javascript
|
|
const hello = function() {return "Hello World!";}
|
|
```
|
|
|
|
**Arrow Functions with One Parameter**
|
|
If a function has only one parameter, you can omit the parentheses. With parentheses: [S1]
|
|
```javascript
|
|
const square = (x) => x * x;
|
|
```
|
|
Without parentheses: [S1]
|
|
```javascript
|
|
const square = x => x * x;
|
|
```
|
|
With parentheses: [S1]
|
|
```javascript
|
|
const hello = (val) => "Hello " + val;
|
|
```
|
|
Without parentheses: [S1]
|
|
```javascript
|
|
const hello = val => "Hello " + val;
|
|
```
|
|
|
|
**Arrow Functions Return Value by Default**
|
|
If the function has only one statement that returns a value, you can remove the brackets and the `return` keyword. This works only if the function has only one statement. [S1]
|
|
```javascript
|
|
const hello = () => "Hello World!";
|
|
```
|
|
|
|
**Arrow Function Parameters**
|
|
If you have parameters, you pass them inside the parentheses: [S1]
|
|
```javascript
|
|
const hello = (val) => "Hello " + val;
|
|
```
|
|
If you have only one parameter, you can skip the parentheses as well: [S1]
|
|
```javascript
|
|
const hello = val => "Hello " + val;
|
|
```
|
|
|
|
**Arrow Functions with No Parameters**
|
|
If there are no parameters, parentheses are required: [S1]
|
|
```javascript
|
|
const hello = () => "Hello World!";
|
|
```
|
|
You can only omit the `return` keyword and the curly brackets if the function is a single statement. Because of this, it might be a good habit to always keep them: [S1]
|
|
```javascript
|
|
// This will return undefined
|
|
const myFunction = (x, y) => { x * y };
|
|
|
|
// This will return undefined
|
|
const myFunction = (x, y) => return x * y;
|
|
|
|
// This will return the expected result
|
|
const myFunction = (x, y) => { return x * y };
|
|
```
|
|
|
|
**Arrow Functions Are Not Declarations**
|
|
Arrow functions are always expressions and must be assigned to a variable. They cannot be used before they are defined: [S1]
|
|
```javascript
|
|
hello(); // Error
|
|
|
|
const hello = () => "Hello";
|
|
```
|
|
Arrow functions must be defined before they are used. [S1]
|
|
|
|
**Arrow Functions and the this Keyword**
|
|
Arrow functions do not have their own `this` value. They inherit `this` from the surrounding code. [S1]
|
|
```javascript
|
|
const person = {
|
|
name: "John",
|
|
greet: function() {
|
|
return this.name;
|
|
}
|
|
};
|
|
```
|
|
Using an arrow function as a method often gives unexpected results: [S1]
|
|
```javascript
|
|
const person = {
|
|
name: "John",
|
|
greet: () => {
|
|
return this.name;
|
|
}
|
|
};
|
|
```
|
|
In this case, `this` does not refer to the person object. [S1]
|
|
|
|
**When to Use Arrow Functions**
|
|
For short functions; for callbacks and array methods; when you do not need your own `this`. [S1]
|
|
|
|
**When Not to Use Arrow Functions**
|
|
As object methods; when you need your own `this`; when using function declarations. [S1]
|
|
|
|
**Common Mistakes**
|
|
Forgetting parentheses rules (parentheses are required for zero or multiple parameters); using arrow functions as methods (arrow functions do not bind `this`); expecting hoisting (arrow functions are not hoisted). [S1]
|
|
|
|
## ⚖️ 비교 및 선택 기준 (Comparison & decision criteria)
|
|
| Situation | Use arrow function? | Reason |
|
|
| --- | --- | --- |
|
|
| Short functions, callbacks, array methods | Yes | Concise syntax; no own `this` needed | [S1]
|
|
| Object methods | No | Arrow functions do not bind `this` to the object | [S1]
|
|
| Need your own `this` | No | Arrow `this` is inherited from surrounding code | [S1]
|
|
| Function declarations | No | Arrow functions are always expressions, not declarations | [S1]
|
|
|
|
## 🛠️ 적용 사례 (Applied in summary)
|
|
The page's own snippets are the canonical applied examples — `multiply`, `square`, and `hello` concise forms, plus the `person.greet` method comparison demonstrating the `this` pitfall. No external project/commit applications found in the source.
|
|
|
|
## 💻 코드 패턴 (Code patterns)
|
|
Concise single-expression arrow:
|
|
```javascript
|
|
const multiply = (a, b) => a * b;
|
|
```
|
|
Single-parameter without parentheses:
|
|
```javascript
|
|
const square = x => x * x;
|
|
```
|
|
Explicit-return habit (avoids accidental undefined):
|
|
```javascript
|
|
const myFunction = (x, y) => { return x * y };
|
|
```
|
|
|
|
## ⚖️ 모순 및 업데이트 (Contradictions & updates)
|
|
No contradictions found in the source.
|
|
|
|
## ✅ 검증 상태 및 신뢰도
|
|
- **상태:** draft
|
|
- **검증 단계:** conceptual (실제 적용 사례 발견 시 applied/validated로 승격 가능)
|
|
- **출처 신뢰도:** B (W3Schools — widely used educational reference, not a primary standards body)
|
|
- **신뢰 점수:** 0.88
|
|
- **중복 검사 결과:** 신규 생성 (New discovery)
|
|
|
|
## 🔗 지식 그래프 (Knowledge Graph)
|
|
- **상위/루트:** [[JavaScript Tutorial]]
|
|
- **관련 개념:** [[JavaScript Function Expressions]], [[JavaScript Function Arguments]], [[JavaScript Object this]]
|
|
- **참조 맥락:** Referenced when writing concise callbacks and when reasoning about `this` binding in object methods.
|
|
|
|
## 📚 출처 (Sources)
|
|
- [S1] W3Schools — JavaScript Arrow Functions — https://www.w3schools.com/js/js_arrow_function.asp
|
|
|
|
## 📝 변경 이력 (Change history)
|
|
- 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript Arrow Functions" page (Astra wiki-curation, P-Reinforce v3.1 format).
|