Function Parameters

Introduction

Functions often need information to perform a specific task. This information is passed to a function through parameters.

A function parameter is a variable listed in the function definition that receives a value when the function is called. The values supplied during the function call are known as arguments.

Parameters make functions flexible and reusable because the same function can work with different values without changing its code.

Function parameters are widely used in JavaScript, Node.js, React, Angular, Vue.js, and automation frameworks such as Selenium, Playwright, and Cypress.

For automation engineers, parameters allow reusable functions for browser actions, API requests, validations, login operations, and data-driven testing.


What are Function Parameters?

Function parameters are variables declared inside the parentheses of a function definition.

When the function is called, the passed values are assigned to these parameters.

Syntax

function functionName(parameter1, parameter2) {

    // Code

}

Parameters vs Arguments

  • Parameters are variables defined in the function declaration.

  • Arguments are the actual values passed to the function.

Example:

function greet(name) {

    console.log("Hello " + name);

}

greet("John");

Here:

  • name is the parameter.

  • "John" is the argument.

Output

Hello John

Function with One Parameter

function square(number) {

    console.log(number * number);

}

square(5);

Output

25

Function with Multiple Parameters

A function can accept multiple parameters separated by commas.

function add(num1, num2) {

    console.log(num1 + num2);

}

add(10, 20);

Output

30

Function with Different Arguments

The same function can work with different values.

function multiply(a, b) {

    console.log(a * b);

}

multiply(4, 5);

multiply(7, 8);

Output

20
56

Passing Strings as Arguments

function welcome(name) {

    console.log("Welcome " + name);

}

welcome("Alice");

Output

Welcome Alice

Passing Different Data Types

Parameters can receive any JavaScript data type.

function display(value) {

    console.log(value);

}

display(100);

display("JavaScript");

display(true);

Output

100
JavaScript
true

Real-World Example

Calculate the total price.

function calculateTotal(price, quantity) {

    console.log(price * quantity);

}

calculateTotal(500, 3);

Output

1500

Another example:

Display employee information.

function employeeDetails(name, department) {

    console.log("Employee: " + name);

    console.log("Department: " + department);

}

employeeDetails("Rahul", "QA");

Output

Employee: Rahul
Department: QA

Automation Testing Example

Function parameters make automation scripts reusable by allowing different input values.

Playwright Example

Launch a browser.

function launchBrowser(browserName) {

    console.log("Launching " + browserName);

}

launchBrowser("Chromium");

Output

Launching Chromium

Selenium Example

Open a website.

function openApplication(url) {

    console.log("Opening: " + url);

}

openApplication("https://example.com");

Output

Opening: https://example.com

Cypress Example

Visit an environment.

function visitEnvironment(environment) {

    console.log("Visiting " + environment + " environment");

}

visitEnvironment("QA");

Output

Visiting QA environment

API Testing Example

Send an API request.

function sendRequest(method, endpoint) {

    console.log(method + " " + endpoint);

}

sendRequest("GET", "/users");

Output

GET /users

Data-Driven Testing Example

Validate user login.

function login(username, password) {

    console.log("Logging in: " + username);

}

login("admin", "admin123");

Output

Logging in: admin

Common Mistakes

Passing Fewer Arguments

function add(a, b) {

    console.log(a + b);

}

add(10);

Output

NaN

The second parameter (b) receives undefined, resulting in NaN.


Misspelling Parameter Names

Incorrect:

function greet(name) {

    console.log(nam);

}

greet("John");

Output

ReferenceError

Always use the correct parameter name.


Passing Arguments in the Wrong Order

function divide(a, b) {

    console.log(a / b);

}

divide(2, 20);

Output

0.1

If you intended to divide 20 by 2, the arguments should be:

divide(20, 2);

Output

10

Best Practices

Use Meaningful Parameter Names

Instead of:

function calculate(a, b) {

}

Use:

function calculateTotal(price, quantity) {

}

Keep the Number of Parameters Reasonable

Avoid functions with too many parameters. If a function requires many inputs, consider passing an object instead.


Validate Input When Necessary

Check that parameters contain valid values before using them, especially when working with user input or external data.


Reuse Functions

Create general-purpose functions that accept different arguments instead of writing separate functions for similar tasks.


Conclusion

Function parameters make JavaScript functions reusable and flexible. By passing different arguments, the same function can perform a variety of tasks without changing its implementation.

Understanding the difference between parameters and arguments is essential for writing clean and maintainable code.

For automation engineers, function parameters simplify browser automation, API testing, login utilities, validation methods, and data-driven testing by allowing reusable functions that work with different input values.


Frequently Asked Questions (FAQs)

What is a function parameter?

A function parameter is a variable declared in a function definition that receives a value when the function is called.


What is an argument?

An argument is the actual value passed to a function.


Can a function have multiple parameters?

Yes.

function add(a, b) {

    console.log(a + b);

}

Can parameters accept different data types?

Yes. Parameters can receive numbers, strings, booleans, objects, arrays, functions, and other JavaScript data types.


What happens if fewer arguments are passed?

The missing parameters receive the value undefined.


Why are function parameters important in automation testing?

Automation engineers use function parameters to create reusable functions for browser actions, API requests, login operations, validations, environment configurations, and data-driven test execution.


Key Takeaways

  • Function parameters receive values passed during a function call.

  • The values passed to a function are called arguments.

  • A function can have one or multiple parameters.

  • Parameters can receive any JavaScript data type.

  • The same function can work with different arguments.

  • Missing arguments result in parameters receiving undefined.

  • Use meaningful parameter names for better readability.

  • Keep functions reusable and focused on a single task.

  • Function parameters are widely used in JavaScript and automation testing.

  • Mastering function parameters helps you write flexible, reusable, and maintainable JavaScript code.