--- id: javascript-function-expressions title: "JavaScript Function Expressions" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["JS function expressions", "anonymous functions", "function declaration vs expression", "hoisting", "named function expression", "callbacks"] 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", "expressions", "hoisting"] raw_sources: ["https://www.w3schools.com/js/js_function_expressions.asp"] applied_in: [] github_commit: "" --- # [[JavaScript Function Expressions]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) A function expression is a function stored in a variable β€” it behaves like a value (great for callbacks) but, unlike a function declaration, it is not hoisted and cannot be called before it is defined. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **A function expression is a function stored in a variable.** [S1] - **Often anonymous** β€” functions stored in variables do not need names; the variable name is used to call the function. They can still be named. [S1] - **It is a statement** β€” a function expression usually ends with a semicolon. [S1] - **Used as a value** β€” because it is stored in a variable, it can be passed to other functions (callbacks). [S1] - **Hoisting difference** β€” function declarations are hoisted and can be called before they are defined; function expressions are not hoisted and cannot. [S1] ## 🧩 μΆ”μΆœλœ νŒ¨ν„΄ (Extracted patterns) - **Store-then-call** β€” assign a function to a `const`, then invoke through the variable name (`sayHello()`). [S1] - **Callback passing** β€” pass a function expression as an argument so another function can invoke it (`run(sayHello)`). [S1] - **Define-before-use ordering** β€” because expressions are not hoisted, declare them above the point of first call. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) **What is a Function Expression?** A function expression is a function stored in a variable. A standard function: [S1] ```javascript function multiply(a, b) { return a * b; } ``` A function expression: [S1] ```javascript const multiply = function(a, b) { return a * b; }; ``` After storing in a variable, the variable can be used as a function: [S1] ```javascript let z = multiply(4, 3); ``` **Anonymous Functions** Function expressions commonly create anonymous functions. The function is actually a function without a name; functions stored in variables do not need names β€” the variable name is used to call the function. However, function expressions can also be named: [S1] ```javascript const add = function add(a, b) {return a + b;}; ``` **Function Expressions Use Semicolons** A function expression is a JavaScript statement. That is why it usually ends with a semicolon. [S1] ```javascript const add = function(a, b) { return a + b; }; ``` **Functions Stored in Variables** Because a function expression is stored in a variable, it can be used like a value. This is useful when passing functions to other functions (callbacks). [S1] ```javascript function run(fn) { return fn(); } const sayHello = function() { return "Hello"; }; run(sayHello); ``` **Functions vs. Function Expressions** JavaScript functions are defined with the `function` keyword and can be defined in different ways. They both work the same when you call them; the difference is when they become available in your code. [S1] **Function Declarations vs Function Expression** A function declaration uses the `function` keyword, has a function name, parameters in parentheses, and a code block in brackets: [S1] ```javascript function add(a, b) {return a + b;} ``` A function expression uses the `function` keyword, parameters in parentheses, and a code block in brackets: [S1] ```javascript const add = function(a, b) {return a + b;}; ``` Example: [S1] ```javascript const sayHello = function() { return "Hello World"; }; sayHello(); ``` The function above is stored in the variable `sayHello`. To run it, you call `sayHello()`. [S1] **Hoisting** Function declarations can be called before they are defined. Function expressions can not be called before they are defined. Function declarations are hoisted: [S1] ```javascript let sum = add(2, 3); // Will work function add(a, b) {return a + b;} ``` Function expressions are not hoisted: [S1] ```javascript let sum = add(2, 3); // β›” Will generate error const add = function (a, b) {return a + b;}; ``` **Key Differences** Syntax: function declarations require a name; function expressions can be anonymous. Hoisting: function declarations are hoisted, function expressions are not. Flexibility: function declarations offer more flexibility in how and where they are used. [S1] **When to Use Each** Use function declarations for general-purpose functions; use function expressions when assigning functions to variables; use function expressions in callbacks and event handlers. [S1] **Later in this Tutorial** Function expressions enable several advanced techniques: arrow functions (modern concise `=>` syntax for function expressions), callbacks (passing functions as arguments), closures (accessing variables from the containing scope), and IIFEs (functions that execute immediately upon definition). [S1] **Common Mistakes** Forgetting the semicolon (expressions are statements and should end with semicolons); expecting hoisting (expressions are not hoisted, cannot call before definition); confusing reference and call (`sayHello` is the function, `sayHello()` calls it); confusing function names and variables (in expressions, the variable name provides the function reference). [S1] ## βš–οΈ 비ꡐ 및 선택 κΈ°μ€€ (Comparison & decision criteria) | Aspect | Function Declaration | Function Expression | | --- | --- | --- | | Name | Required | Can be anonymous | [S1] | Hoisting | Hoisted (callable before definition) | Not hoisted (must define first) | [S1] | Flexibility | More flexible in how/where used | Used as a value (callbacks, variables) | [S1] | When to use | General-purpose functions | Variable assignment, callbacks, event handlers | [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) The page's own snippets are the canonical applied examples β€” `multiply`/`add` expressions, the `run(sayHello)` callback pattern, and the hoisting error/success comparison. No external project/commit applications found in the source. ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Function expression stored in a variable: ```javascript const multiply = function(a, b) { return a * b; }; ``` Passing a function expression as a callback: ```javascript function run(fn) { return fn(); } const sayHello = function() { return "Hello"; }; run(sayHello); ``` ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (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 Arrow Functions]], [[JavaScript Function Arguments]], [[JavaScript Objects]] - **μ°Έμ‘° λ§₯락:** Referenced when choosing between declaration and expression syntax, and as the foundation for arrow functions, callbacks, and closures. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” JavaScript Function Expressions β€” https://www.w3schools.com/js/js_function_expressions.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript Function Expressions" page (Astra wiki-curation, P-Reinforce v3.1 format).