In JavaScript, a function can take parameters to make it more dynamic and reusable. Parameters are placeholders for values passed into the function when it's called. Here's an example of how to define and use a function with parameters:
function functionName(parameter1, parameter2) {
// Code to be executed
console.log(`Parameter 1: ${parameter1}, Parameter 2: ${parameter2}`);
}
Example
<script>
// Define a function with parameters
function greet(name, age) {
console.log(`Hello, ${name}! You are ${age} years old.`);
}
// Call the function with arguments
greet("Alice", 25); // Output: Hello, Alice! You are 25 years old.
greet("Bob", 30); // Output: Hello, Bob! You are 30 years old.
greet("cgva",9999);
</script>