--- id: javascript-function-invocation title: "JavaScript Function Invocation" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["calling a function", "function invocation", "invoke", "() operator", "calling vs referencing", "function call"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.87 created_at: 2026-06-23 updated_at: 2026-06-23 review_reason: "" merge_history: [] tags: ["javascript", "js", "web", "frontend", "w3schools", "functions", "invocation"] raw_sources: ["https://www.w3schools.com/js/js_function_invocation.asp"] applied_in: [] github_commit: "" --- # [[JavaScript Function Invocation]] ## π― ν μ€ ν΅μ°° (One-line insight) The code inside a function does not run when the function is defined β it runs when something *invokes* it, and the `()` operator is what invokes a function. [S1] ## π§ ν΅μ¬ κ°λ (Core concepts) - **Definition vs invocation** β code inside a function is NOT executed when the function is defined; it executes when "something" invokes the function. [S1] - **Invoke vs call** β the term "invoke" is common because a function can be invoked without being called directly. [S1] - **The `()` operator invokes a function** β appending parentheses to a function name runs it. [S1] - **Calling vs referencing** β `sayHello` refers to the function itself (and returns the function); `sayHello()` refers to the function result (and returns the result). [S1] - **Store the returned value** β when a function returns a value you can store it in a variable. [S1] ## π§© μΆμΆλ ν¨ν΄ (Extracted patterns) - **Invoke with `()`** β `sayHello()` runs the body and yields its return value; omitting `()` yields the function object instead. [S1] - **Capture the result** β `let greeting = sayHello();` keeps the returned value for reuse. [S1] - **Invoke on an event** β wire a function to a UI event (e.g. `onclick="showHello()"`) so it runs on interaction. [S1] ## π μΈλΆ λ΄μ© (Details) **Calling a Function** β a defined function: [S1] ```javascript function sayHello() { return "Hello World"; } ``` The code inside a function is **NOT executed** when the function is **defined**. The code inside a function **will execute** when "something" **invokes** the function. It is common to use the term **invoke**, because a function can be invoked without being called. [S1] Invoke the function with `()`: [S1] ```javascript function sayHello() { return "Hello World"; } sayHello(); ``` **Using the Returned Value** β when a function returns a value, you can store the value in a variable: [S1] ```javascript function sayHello() { return "Hello World"; } let greeting = sayHello(); ``` Log the result: [S1] ```javascript function sayHello() { return "Hello World"; } console.log(sayHello()); ``` **Displaying the Result** β invoke a function and write its result into the page: [S1] ```javascript
``` **Calling a Function Many Times** β the same function can be invoked repeatedly: [S1] ```javascript function sayHello() { return "Hello World"; } let a = sayHello(); let b = sayHello(); let c = sayHello(); ``` A converting function invoked with an argument: [S1] ```javascript // Convert Fahrenheit to Celsius: function toCelsius(fahrenheit) { return (5/9) * (fahrenheit-32); } // Call the toCelcius() function let value = toCelsius(77); ``` **Calling vs Referencing a Function** β `() ` invokes; without it you get the function itself: [S1] ```javascript function toCelsius(fahrenheit) { return (5/9) * (fahrenheit-32); } let value = toCelsius; ``` ```javascript function sayHello() { return "Hello World"; } let text = sayHello; ``` This is an important difference: `sayHello` refers to the **function itself** (it returns the function); `sayHello()` refers to the **function result** (it returns the result). The `()` operator invokes a function. [S1] **Functions Can Be Called from Anywhere** β define one function and invoke it from another (e.g. on a button click): [S1] ```javascript function sayHello() { return "Hello World"; } function showHello() { document.getElementById("demo").innerHTML = sayHello(); } ``` ```javascript ``` **Common Mistakes** β [S1] - **Forgetting Parentheses `()`** β `sayHello` does not run the function; you must use `sayHello()`. - **Expecting Return** β some functions do not return a value. - **Expecting Output** β if a function returns a value, you must display it to see it. ## π οΈ μ μ© μ¬λ‘ (Applied in summary) The page's own snippets are the canonical applied examples β invoking `sayHello()`, capturing its return, logging it, writing it to `#demo`, calling it many times, and triggering `showHello()` from a button. No external project/commit applications found in the source. ## π» μ½λ ν¨ν΄ (Code patterns) Invoke and capture the result (language: JavaScript): ```javascript let greeting = sayHello(); ``` Reference vs invoke: ```javascript let text = sayHello; // the function itself let result = sayHello(); // the function's result ``` Invoke on a UI event: ```javascript function showHello() { document.getElementById("demo").innerHTML = 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.87 - **μ€λ³΅ κ²μ¬ κ²°κ³Ό:** μ κ· μμ± (New discovery) ## π μ§μ κ·Έλν (Knowledge Graph) - **μμ/루νΈ:** [[JavaScript Tutorial]] - **κ΄λ ¨ κ°λ :** [[JavaScript Functions]], [[JavaScript Function Definitions]], [[JavaScript Function Parameters]], [[JavaScript Function Returns]] - **μ°Έμ‘° λ§₯λ½:** Referenced whenever deciding how and when a defined function's body should actually run. ## π μΆμ² (Sources) - [S1] W3Schools β JavaScript Function Invocation β https://www.w3schools.com/js/js_function_invocation.asp ## π λ³κ²½ μ΄λ ₯ (Change history) - 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript Function Invocation" page (Astra wiki-curation, P-Reinforce v3.1 format).