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>
219 lines
8.6 KiB
Markdown
219 lines
8.6 KiB
Markdown
---
|
|
id: javascript-function-arguments
|
|
title: "JavaScript Function Arguments"
|
|
category: "Frontend"
|
|
status: "draft"
|
|
verification_status: "conceptual"
|
|
canonical_id: ""
|
|
aliases: ["JS function arguments", "function parameters", "arguments object", "default parameters", "rest parameter", "pass by value", "pass by reference"]
|
|
duplicate_of: ""
|
|
source_trust_level: "B"
|
|
confidence_score: 0.89
|
|
created_at: 2026-06-23
|
|
updated_at: 2026-06-23
|
|
review_reason: ""
|
|
merge_history: []
|
|
tags: ["javascript", "js", "web", "frontend", "w3schools", "functions", "arguments", "parameters"]
|
|
raw_sources: ["https://www.w3schools.com/js/js_function_arguments.asp"]
|
|
applied_in: []
|
|
github_commit: ""
|
|
---
|
|
|
|
# [[JavaScript Function Arguments]]
|
|
|
|
## 🎯 한 줄 통찰 (One-line insight)
|
|
Parameters are the names listed in a function definition; arguments are the real values passed in — and JavaScript never checks their type or count, so missing arguments become `undefined`. [S1]
|
|
|
|
## 🧠 핵심 개념 (Core concepts)
|
|
- **Parameters vs. arguments** — parameters are the *names* in the function definition; arguments are the *real values* passed to and received by the function. [S1]
|
|
- **Order matters** — arguments are assigned to parameters in the order they appear. [S1]
|
|
- **No type or count checking** — JavaScript function definitions do not specify data types, do not perform type checking, and do not check the number of arguments received. [S1]
|
|
- **Missing arguments become `undefined`** — calling with fewer arguments than parameters leaves the missing ones `undefined`. [S1]
|
|
- **`arguments` object** — every function has a built-in `arguments` object containing an array of the arguments used at call time, reachable even when more arguments are passed than declared. [S1]
|
|
- **Pass by value vs. reference** — primitives are passed by value (changes are not reflected outside); objects behave like they are passed by reference (changing an object property changes the original). [S1]
|
|
|
|
## 🧩 추출된 패턴 (Extracted patterns)
|
|
- **Variadic via `arguments`** — loop over `arguments.length` to handle an indefinite number of inputs without declaring parameters. [S1]
|
|
- **Default parameter guard** — set a default with `y = 10` in the signature (ES2015), or the older `if (y === undefined) y = 2;` pattern. [S1]
|
|
- **Rest parameter collection** — `...args` gathers an indefinite number of arguments into a real array for iteration with `for...of`. [S1]
|
|
|
|
## 📖 세부 내용 (Details)
|
|
**Parameters vs. Arguments**
|
|
Function **parameters** are the names listed in the function definition. **Arguments** are the real values passed to, and received by, the function. [S1]
|
|
```javascript
|
|
function multiply(a, b) {
|
|
return a * b;
|
|
}
|
|
|
|
let result = multiply(4, 5);
|
|
```
|
|
Here `a` and `b` are parameters; `4` and `5` are arguments. The argument `4` is assigned to parameter `a` and `5` to parameter `b`. [S1]
|
|
|
|
**The Arguments Object**
|
|
JavaScript functions have a built-in `arguments` object that contains an array of the arguments used when the function was invoked. This lets a function find, for instance, the highest value in a list of numbers: [S1]
|
|
```javascript
|
|
x = findMax(1, 123, 500, 115, 44, 88);
|
|
|
|
function findMax() {
|
|
let max = -Infinity;
|
|
for (let i = 0; i < arguments.length; i++) {
|
|
if (arguments[i] > max) {
|
|
max = arguments[i];
|
|
}
|
|
}
|
|
return max;
|
|
}
|
|
```
|
|
Or sum all input values: [S1]
|
|
```javascript
|
|
x = sumAll(1, 123, 500, 115, 44, 88);
|
|
|
|
function sumAll() {
|
|
let sum = 0;
|
|
for (let i = 0; i < arguments.length; i++) {
|
|
sum += arguments[i];
|
|
}
|
|
return sum;
|
|
}
|
|
```
|
|
If a function is called with too many arguments (more than declared), those arguments can still be reached using the `arguments` object. [S1]
|
|
|
|
**The Order of Arguments Matters**
|
|
Arguments are assigned to parameters in the order they appear. [S1]
|
|
```javascript
|
|
function subtract(a, b) {
|
|
return a - b;
|
|
}
|
|
|
|
let x1 = subtract(10, 5);
|
|
let x2 = subtract(5, 10);
|
|
```
|
|
The two calls return different results because the order is different. [S1]
|
|
|
|
**Arguments Can Be Variables**
|
|
Arguments do not have to be values; they can also be variables. [S1]
|
|
```javascript
|
|
let x = 5;
|
|
let y = 6;
|
|
|
|
function multiply(a, b) {
|
|
return a * b;
|
|
}
|
|
|
|
multiply(x, y);
|
|
```
|
|
|
|
**Argument Rules**
|
|
JavaScript function definitions do not specify data types for arguments. JavaScript functions do not perform type checking on the passed arguments. JavaScript functions do not check the number of arguments received. [S1]
|
|
|
|
**Incorrect Arguments**
|
|
Incorrect arguments can return incorrect answers: [S1]
|
|
```javascript
|
|
function toCelsius(fahrenheit) {
|
|
return (5/9) * (fahrenheit-32);
|
|
}
|
|
|
|
let value = toCelsius("John");
|
|
```
|
|
|
|
**Missing Arguments**
|
|
If a function is called with fewer arguments than parameters, the missing values become `undefined`. [S1]
|
|
```javascript
|
|
function multiply(a, b) {
|
|
return a * b;
|
|
}
|
|
|
|
multiply(4);
|
|
```
|
|
Here `b` is `undefined`, so the result is `NaN`. A JavaScript function does not perform any checking on parameter values. [S1]
|
|
|
|
**Default Parameters**
|
|
If a function is called with missing arguments, the missing values are set to `undefined`. Sometimes it is better to assign a default value to the parameter: [S1]
|
|
```javascript
|
|
function myFunction(x, y) {
|
|
if (y === undefined) {
|
|
y = 2;
|
|
}
|
|
}
|
|
```
|
|
|
|
**Default Parameter Values**
|
|
ECMAScript 2015 allows function parameters to have default values. The default value is used if no argument is provided. If `y` is not passed or `undefined`, then `y = 10`: [S1]
|
|
```javascript
|
|
function myFunction(x, y = 10) {
|
|
return x + y;
|
|
}
|
|
myFunction(5);
|
|
```
|
|
|
|
**Function Rest Parameter**
|
|
The rest parameter (`...`) allows a function to treat an indefinite number of arguments as an array: [S1]
|
|
```javascript
|
|
function sum(...args) {
|
|
let sum = 0;
|
|
for (let arg of args) sum += arg;
|
|
return sum;
|
|
}
|
|
|
|
let x = sum(4, 9, 16, 25, 29, 100, 66, 77);
|
|
```
|
|
|
|
**Arguments are Passed by Value**
|
|
The parameters in a function call are the function's arguments. JavaScript arguments are passed by value: the function only gets to know the values, not the argument's locations. If a function changes an argument's value, it does not change the parameter's original value. Changes to arguments are not visible (reflected) outside the function. [S1]
|
|
|
|
**Objects are Passed by Reference**
|
|
In JavaScript, object references are values. Because of this, objects will behave like they are passed by reference: if a function changes an object property, it changes the original value. Changes to object properties are visible (reflected) outside the function. [S1]
|
|
|
|
**Common Mistakes**
|
|
Confusing parameters and arguments (parameters are names, arguments are values); forgetting the order (arguments are assigned by position); missing arguments (use default values to avoid `undefined`). [S1]
|
|
|
|
## 🛠️ 적용 사례 (Applied in summary)
|
|
The page's own snippets are the canonical applied examples — `multiply`/`subtract` for parameter binding, `findMax`/`sumAll` for the `arguments` object, and `sum(...args)` for the rest parameter. No external project/commit applications found in the source.
|
|
|
|
## 💻 코드 패턴 (Code patterns)
|
|
Default parameter value (ES2015):
|
|
```javascript
|
|
function myFunction(x, y = 10) {
|
|
return x + y;
|
|
}
|
|
```
|
|
Variadic via rest parameter:
|
|
```javascript
|
|
function sum(...args) {
|
|
let sum = 0;
|
|
for (let arg of args) sum += arg;
|
|
return sum;
|
|
}
|
|
```
|
|
Variadic via the `arguments` object:
|
|
```javascript
|
|
function sumAll() {
|
|
let sum = 0;
|
|
for (let i = 0; i < arguments.length; i++) {
|
|
sum += arguments[i];
|
|
}
|
|
return sum;
|
|
}
|
|
```
|
|
|
|
## ⚖️ 모순 및 업데이트 (Contradictions & updates)
|
|
No contradictions found in the source. The page notes a version distinction: default parameter values are an ECMAScript 2015 feature, contrasted with the older `if (y === undefined)` guard pattern. [S1]
|
|
|
|
## ✅ 검증 상태 및 신뢰도
|
|
- **상태:** draft
|
|
- **검증 단계:** conceptual (실제 적용 사례 발견 시 applied/validated로 승격 가능)
|
|
- **출처 신뢰도:** B (W3Schools — widely used educational reference, not a primary standards body)
|
|
- **신뢰 점수:** 0.89
|
|
- **중복 검사 결과:** 신규 생성 (New discovery)
|
|
|
|
## 🔗 지식 그래프 (Knowledge Graph)
|
|
- **상위/루트:** [[JavaScript Tutorial]]
|
|
- **관련 개념:** [[JavaScript Function Expressions]], [[JavaScript Arrow Functions]], [[JavaScript Objects]]
|
|
- **참조 맥락:** Referenced whenever defining functions and reasoning about how values reach them, including variadic functions and defaults.
|
|
|
|
## 📚 출처 (Sources)
|
|
- [S1] W3Schools — JavaScript Function Arguments — https://www.w3schools.com/js/js_function_arguments.asp
|
|
|
|
## 📝 변경 이력 (Change history)
|
|
- 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript Function Arguments" page (Astra wiki-curation, P-Reinforce v3.1 format).
|