-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
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 Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Window
JS Screen
JS Location
JS History
JS Navigator
JS Popup Alert
JS Timing
JS Cookies
Web Storage API
JS Web APIs
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Canvas
JS Graphics & Charts
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 Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Window
JS Screen
JS Location
JS History
JS Navigator
JS Popup Alert
JS Timing
JS Cookies
Web Storage API
JS Web APIs
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Canvas
JS Graphics & Charts
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.
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).
function add(a, b) {
return a + b;
}
console.log(add(5, 3)); // Output: 8
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
...
)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
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
Term | Meaning |
---|---|
Parameter | Variable in function definition |
Argument | Actual value passed to function |
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)
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?
Q1: Which of the following is a valid default parameter?
Q2: What does the rest parameter syntax (...args) do?
Q3: Which function correctly uses destructuring in parameters?
Q4: What happens when a function receives more arguments than parameters?
Q5: Which of these keywords is used for variable-length arguments?
Q6: Which is NOT true about function parameters?
Q7: Which of the following is true about rest parameters?