Function parameters are names listed in a function definition. They act like placeholders for values that the function will receive when called.
Click to reveal answer
beginner
How do you define a function with two parameters named a and b?
You write: <br><pre>function example(a, b) {<br> // code here<br>}</pre>
Click to reveal answer
intermediate
What happens if you call a function with fewer arguments than parameters?
The missing parameters get the value undefined. You can check for this inside the function to handle missing values.
Click to reveal answer
intermediate
What is a default parameter value in JavaScript functions?
A default parameter value is a value assigned to a parameter in the function definition. It is used if no argument is passed for that parameter when the function is called.
Click to reveal answer
beginner
How do you write a function with a default parameter value of 10 for parameter num?
You write: <br><pre>function example(num = 10) {<br> // code here<br>}</pre>
Click to reveal answer
What will be the value of b when calling function test(a, b) {} as test(5)?
Anull
B5
Cundefined
D0
✗ Incorrect
If you call a function with fewer arguments than parameters, the missing parameters get the value undefined.
How do you assign a default value of 100 to a parameter x in a function?
Afunction f(x <- 100) {}
Bfunction f(x: 100) {}
Cfunction f(x == 100) {}
Dfunction f(x = 100) {}
✗ Incorrect
The correct syntax for default parameters in JavaScript is function f(x = 100) {}.
Which of the following is true about function parameters?
AParameters are placeholders for values passed to functions.
BParameters store the return value of a function.
CParameters are only used outside the function.
DParameters must always have default values.
✗ Incorrect
Parameters are names used inside function definitions to receive values when the function is called.
What happens if you call a function with more arguments than parameters?
AIt causes a syntax error.
BExtra arguments are ignored unless accessed via special objects.
CThe function automatically creates new parameters.
DThe function stops running.
✗ Incorrect
Extra arguments are ignored by named parameters but can be accessed using the arguments object or rest parameters.
Which keyword is used to collect multiple function arguments into an array?
Arest parameter (...)
Bspread operator (...)
Cdefault parameter
Darguments
✗ Incorrect
The rest parameter syntax ...name collects multiple arguments into an array.
Explain what function parameters are and how they work in JavaScript.
Think about how functions get information to work with.
You got /3 concepts.
Describe how to use default parameter values and why they are useful.
Consider what happens when a function is called without some arguments.
You got /3 concepts.
Practice
(1/5)
1.
What is the purpose of function parameters in JavaScript?
Choose the best explanation.
easy
A. They make functions run faster.
B. They create new functions automatically.
C. They stop a function from running.
D. They allow you to send information into functions to use inside.
Solution
Step 1: Understand what parameters do
Parameters let you pass values into a function so it can use them inside.
Step 2: Evaluate the options
Only "They allow you to send information into functions to use inside." correctly explains this purpose. Others describe unrelated or incorrect effects.
Final Answer:
They allow you to send information into functions to use inside. -> Option D
Quick Check:
Function parameters = input values [OK]
Hint: Parameters are inputs to functions, like filling a form [OK]
Common Mistakes:
Thinking parameters create functions
Believing parameters stop functions
Assuming parameters speed up functions
2.
Which of the following is the correct way to define a function with two parameters a and b in JavaScript?
function add(???) { return a + b; }
easy
A. (a, b)
B. a b
C. a, b
D. [a, b]
Solution
Step 1: Recall function parameter syntax
Parameters are listed separated by commas inside the function parentheses.
Step 2: Identify the correct parameter list
(a, b) correctly lists parameters as 'a, b' inside parentheses. 'a, b' without parentheses is invalid syntax. Extra parentheses inside parentheses are not used.
Final Answer:
(a, b) -> Option A
Quick Check:
Parameters separated by commas inside () [OK]
Hint: List parameters separated by commas inside function parentheses [OK]
Common Mistakes:
Using spaces instead of commas
Adding extra parentheses around parameters
Using brackets instead of commas
3.
What will be the output of this code?
function greet(name) {
return `Hello, ${name}!`;
}
console.log(greet('Alice'));
medium
A. "Hello, Alice!"
B. "Hello, name!"
C. undefined
D. Error
Solution
Step 1: Understand function call with argument
The function greet is called with 'Alice' as the argument for parameter name.
Step 2: Evaluate template literal output
The template string inserts the value of name, so it becomes 'Hello, Alice!'.
Final Answer:
"Hello, Alice!" -> Option A
Quick Check:
Parameter value replaces placeholder [OK]
Hint: Parameter value replaces variable inside template strings [OK]
Common Mistakes:
Thinking parameter name is printed literally
Expecting undefined if parameter not used
Confusing string quotes in output
4.
Find the error in this function definition:
function multiply(x, y) {
return x * y;
}
multiply(5);
What will happen when multiply(5) is called?
medium
A. Throws a syntax error
B. Returns 25
C. Returns NaN
D. Returns 5
Solution
Step 1: Check function call with missing argument
The function multiply expects two parameters but is called with only one argument (5).
Step 2: Understand behavior of missing parameter
The second parameter y is undefined, so x * y becomes 5 * undefined, which results in NaN (Not a Number).
Final Answer:
Returns NaN -> Option C
Quick Check:
Missing argument causes NaN in math operations [OK]
Hint: Missing parameters become undefined, causing NaN in math [OK]
Common Mistakes:
Expecting default value 0 for missing parameters
Thinking it throws an error
Assuming multiply returns the single argument
5.
Consider this function that uses default parameters: