JavaScript

coding learning websites codepractice

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?


JavaScript

online coding class codepractice

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

Go Back Top