What are function parameters in JavaScript?
A
Values returned by a function B
Variables listed in the function definition C
Values used for loops D
The function’s return values
Analysis & Theory
Parameters are the variable names listed in the function definition.
What does the following function return?
```js
function sum(a, b) {
return a + b;
}
sum(2, 3);
```
A
23 B
5 C
`undefined` D
`NaN`
Analysis & Theory
2 + 3 equals 5, so the function returns 5.
What happens if you call a function with fewer arguments than parameters?
A
It throws an error B
Missing parameters are `undefined` C
It fills with zeroes D
It loops infinitely
Analysis & Theory
Missing parameters default to `undefined` in JavaScript.
What will this function return?
```js
function greet(name = 'Guest') {
return 'Hello ' + name;
}
greet();
```
A
`Hello undefined` B
`Hello null` C
`Hello Guest` D
Error
Analysis & Theory
Default parameters are used when no argument is provided. So it returns `'Hello Guest'`.
What does the `...` (spread) operator do in function parameters?
A
It duplicates parameters B
It calls another function C
It gathers remaining arguments into an array D
It makes all parameters optional
Analysis & Theory
The `...` rest parameter gathers all remaining arguments into an array.
Which of the following correctly uses rest parameters?
A
function myFunc(...args) {} B
function myFunc(args...) {} C
function(...args) myFunc {} D
function myFunc(...) args {}
Analysis & Theory
`...args` is the correct syntax to define rest parameters.
How many arguments will this function receive?
```js
function test(a, b, c) {
console.log(arguments.length);
}
test(1, 2);
```
A
3 B
2 C
1 D
0
Analysis & Theory
Only 2 arguments are passed, even though 3 parameters are defined.
What is the `arguments` object?
A
A built-in array of passed arguments B
A string list of variables C
A number of parameters D
A special parameter type
Analysis & Theory
`arguments` is an array-like object available inside traditional functions to access passed arguments.
Can arrow functions use the `arguments` object?
A
Yes B
Only in strict mode C
No D
Only in global scope
Analysis & Theory
Arrow functions do not have their own `arguments` object.
Which is TRUE about JavaScript function parameters?
A
All parameters must be used B
They must match the number of arguments C
They are optional by default D
They require type declaration
Analysis & Theory
Function parameters are optional and default to `undefined` if not provided.