Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Parameters and Return Values in JavaScript


In JavaScript, functions can take parameters and return values. Parameters allow functions to accept input, while return values let them output results. Understanding how parameters and return values work is crucial for writing effective functions. This article will explain parameters and return values in JavaScript with examples.

1. What are Parameters?

Parameters are variables listed in the function definition that allow a function to accept input values. These values are passed when the function is called and can be used within the function to perform computations or operations.

Syntax:

      function functionName(parameter1, parameter2) {
        // function body
      }
    

In this syntax, parameter1 and parameter2 are parameters that represent the inputs to the function functionName.

2. Example of Function with Parameters

Here is an example of a function that accepts two parameters, a and b, and returns their sum.

      function add(a, b) {
        return a + b;
      }

      console.log(add(5, 3)); // Output: 8
    

In this example, the function add takes two parameters, a and b, and returns their sum. When the function is called with arguments 5 and 3, it outputs 8.

3. Default Parameters

In JavaScript, you can assign default values to parameters. This is useful when a function is called without providing values for some or all of its parameters.

Syntax:

      function functionName(parameter1 = defaultValue) {
        // function body
      }
    

If the function is called without passing a value for parameter1, it will use the defaultValue.

Example: Function with default parameters

      function greet(name = "Guest") {
        return "Hello, " + name;
      }

      console.log(greet("Alice")); // Output: Hello, Alice
      console.log(greet()); // Output: Hello, Guest
    

In this example, the greet function has a default parameter name with the value "Guest". If no argument is provided, the default value is used.

4. Rest Parameters

Rest parameters allow you to represent an indefinite number of arguments as an array. This is particularly useful when you don't know how many arguments will be passed to the function.

Syntax:

      function functionName(...restParameters) {
        // function body
      }
    

The ... syntax before the parameter name indicates that the parameter will gather all remaining arguments into an array.

Example: Function with rest parameters

      function sum(...numbers) {
        let total = 0;
        for (let num of numbers) {
          total += num;
        }
        return total;
      }

      console.log(sum(1, 2, 3, 4)); // Output: 10
      console.log(sum(5, 5)); // Output: 10
    

In this example, the function sum takes an arbitrary number of arguments using the rest parameter ...numbers and returns their sum.

5. Return Values

A return value is the result that a function outputs when it finishes executing. The return keyword is used to specify the value that the function will return.

Syntax:

      function functionName() {
        return value;
      }
    

The value can be any valid JavaScript expression, such as a string, number, object, or even another function.

6. Example of Function with Return Value

Here is an example of a function that takes two parameters and returns their product.

      function multiply(a, b) {
        return a * b;
      }

      console.log(multiply(4, 5)); // Output: 20
    

In this example, the function multiply takes two parameters, a and b, multiplies them, and returns the result. The result of calling multiply(4, 5) is 20.

7. Returning Multiple Values

JavaScript functions can only return one value. However, you can return multiple values by using an object or an array.

Example: Returning multiple values using an array

      function getPersonInfo() {
        return ["Alice", 30];
      }

      let [name, age] = getPersonInfo();
      console.log(name); // Output: Alice
      console.log(age); // Output: 30
    

In this example, the function getPersonInfo returns an array containing two values: a name and an age. The values are then destructured into individual variables.

Example: Returning multiple values using an object

      function getPersonDetails() {
        return { name: "Alice", age: 30 };
      }

      let person = getPersonDetails();
      console.log(person.name); // Output: Alice
      console.log(person.age); // Output: 30
    

Alternatively, you can return an object that contains multiple properties. In this example, the function getPersonDetails returns an object with name and age properties.

8. Early Return

In JavaScript, you can use the return keyword to exit a function early. This is useful when you want to stop further execution of a function based on a condition.

Example: Early return

      function checkAge(age) {
        if (age < 18) {
          return "You are underage";
        }
        return "You are an adult";
      }

      console.log(checkAge(16)); // Output: You are underage
      console.log(checkAge(20)); // Output: You are an adult
    

In this example, the checkAge function exits early if the age is less than 18, returning a message without executing the rest of the code.

Summary of Parameters and Return Values

Conclusion

Understanding parameters and return values is crucial for working with functions in JavaScript. By passing data into functions using parameters and returning results with the return keyword, you can create flexible and reusable functions that help manage the flow of your code.





Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java