JavaScript

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JavaScript

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

Functions Parameters


📘 JavaScript Function Parameters – Input Values to Functions

In JavaScript, function parameters are placeholders for input values that are passed to a function when it is called. These values help functions work on different data without rewriting the code.


🔹 Basic Parameter Usage

function greet(name) {
  console.log("Hello, " + name);
}

greet("Alice"); // Output: Hello, Alice
  • name is a parameter (in the function definition).

  • "Alice" is an argument (in the function call).


🔹 Multiple Parameters

function add(a, b) {
  return a + b;
}

console.log(add(5, 3)); // Output: 8

🔹 Default Parameters (ES6+)

You can assign a default value to a parameter if no argument is passed:

function greet(name = "Guest") {
  return "Hi " + name;
}

console.log(greet()); // Output: Hi Guest

🔹 Rest Parameters (...)

Use the ... syntax to accept multiple arguments as an array:

function sum(...numbers) {
  return numbers.reduce((a, b) => a + b, 0);
}

console.log(sum(1, 2, 3)); // Output: 6

🔹 Destructured Parameters

You can destructure arrays or objects directly in parameters:

function display({ name, age }) {
  console.log(`${name} is ${age} years old`);
}

display({ name: "Bob", age: 25 }); // Output: Bob is 25 years old

🔹 Parameter vs Arguments

Term Meaning
Parameter Variable in function definition
Argument Actual value passed to function

🔹 Extra or Missing Arguments

JavaScript is lenient:

function show(a, b) {
  console.log(a, b);
}

show(1);           // Output: 1 undefined
show(1, 2, 3, 4);  // Output: 1 2 (extra args ignored)

Practice Questions

Q1. What is the difference between a function parameter and an argument in JavaScript?

Q2. How do you set a default parameter value in a function?

Q3. Write a function that multiplies two numbers with default values of 1 for both.

Q4. What happens when a function is called with fewer arguments than parameters?

Q5. How can you use rest parameters to handle an unknown number of arguments?

Q6. Write a function that returns the sum of all arguments passed using the rest operator.

Q7. How do destructured parameters work when accepting objects as input?

Q8. Create a function that accepts an object and logs its properties using destructuring.

Q9. Can you combine normal parameters with rest parameters? Show an example.

Q10. What is the result of calling a function with more arguments than defined parameters?


Functions Parameters Quiz

Q1: Which of the following is a valid default parameter?

A. function greet(name == "Guest")
B. function greet(name : "Guest")
C. function greet(name = "Guest")
D. function greet(name ? "Guest")

Q2: What does the rest parameter syntax (...args) do?

A. Adds all arguments
B. Converts arguments to array
C. Ignores remaining arguments
D. Removes undefined values

Q3: Which function correctly uses destructuring in parameters?

A. function show({name, age})
B. function show(name, age)
C. function show(name: age)
D. function show(name = age)

Q4: What happens when a function receives more arguments than parameters?

A. Extra arguments cause error
B. Extra arguments are ignored
C. Only first argument is used
D. Extra arguments become null

Q5: Which of these keywords is used for variable-length arguments?

A. many
B. spread
C. rest
D. extra

Q6: Which is NOT true about function parameters?

A. You can assign default values
B. You can use ...rest
C. Destructuring is allowed
D. Parameter names must be unique across functions

Q7: Which of the following is true about rest parameters?

A. Can appear anywhere in the parameter list
B. Must be the last parameter
C. Cannot be used with default values
D. Used only in arrow functions

Go Back Top