--- 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).