Function parameters let you give information to a function so it can work with different values.
Function parameters in Javascript
function functionName(parameter1, parameter2) { // code using parameters }
Parameters are like placeholders for values you give when calling the function.
You can have as many parameters as you need, separated by commas.
name and prints a greeting.function greet(name) { console.log('Hello, ' + name + '!'); }
function add(a, b) { return a + b; }
function sayHello() { console.log('Hello!'); }
This program defines a function that multiplies two numbers given as parameters and prints the result.
function multiply(x, y) { return x * y; } const result = multiply(4, 5); console.log('4 times 5 is', result);
If you call a function without enough parameters, the missing ones become undefined.
You can give default values to parameters to avoid undefined.
Parameters help make functions reusable with different inputs.
Function parameters let you send information into functions.
They act like variables inside the function holding the input values.
Using parameters makes your functions flexible and reusable.
Practice
What is the purpose of function parameters in JavaScript?
Choose the best explanation.
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 DQuick Check:
Function parameters = input values [OK]
- Thinking parameters create functions
- Believing parameters stop functions
- Assuming parameters speed up functions
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; }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 AQuick Check:
Parameters separated by commas inside () [OK]
- Using spaces instead of commas
- Adding extra parentheses around parameters
- Using brackets instead of commas
What will be the output of this code?
function greet(name) {
return `Hello, ${name}!`;
}
console.log(greet('Alice'));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 AQuick Check:
Parameter value replaces placeholder [OK]
- Thinking parameter name is printed literally
- Expecting undefined if parameter not used
- Confusing string quotes in output
Find the error in this function definition:
function multiply(x, y) {
return x * y;
}
multiply(5);What will happen when multiply(5) is called?
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 CQuick Check:
Missing argument causes NaN in math operations [OK]
- Expecting default value 0 for missing parameters
- Thinking it throws an error
- Assuming multiply returns the single argument
Consider this function that uses default parameters:
function calculateTotal(price, tax = 0.1, discount = 0) {
return price + price * tax - discount;
}
console.log(calculateTotal(100, undefined, 5));What is the output of the console.log statement?
Solution
Step 1: Understand default parameters and call
The function has tax default 0.1 and discount default 0. The call passes price=100, tax=undefined, discount=5.Step 2: Evaluate how undefined affects default
Passing undefined for tax triggers the default value 0.1 to be used.Step 3: Calculate the total
Total = 100 + 100 * 0.1 - 5 = 100 + 10 - 5 = 105.Final Answer:
105 -> Option BQuick Check:
Undefined triggers default parameter value [OK]
- Thinking undefined overrides default with undefined
- Ignoring discount in calculation
- Confusing tax and discount values
Ready to prove it?
Pass the test and this pattern moves unknown
