0
0
Javascriptprogramming~5 mins

Function parameters in Javascript

Choose your learning style9 modes available
Introduction

Function parameters let you give information to a function so it can work with different values.

When you want a function to add two numbers you give it the numbers as parameters.
When you want to greet different people, you pass their names as parameters.
When you want to calculate the area of different rectangles, you pass width and height as parameters.
Syntax
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.

Examples
This function takes one parameter name and prints a greeting.
Javascript
function greet(name) {
  console.log('Hello, ' + name + '!');
}
This function takes two parameters and returns their sum.
Javascript
function add(a, b) {
  return a + b;
}
This function has no parameters and always says hello the same way.
Javascript
function sayHello() {
  console.log('Hello!');
}
Sample Program

This program defines a function that multiplies two numbers given as parameters and prints the result.

Javascript
function multiply(x, y) {
  return x * y;
}

const result = multiply(4, 5);
console.log('4 times 5 is', result);
OutputSuccess
Important Notes

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.

Summary

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.