Sunday, 1 December 2024

Returning values from a function in JavaScript

Functions can return a value using the return statement. This allows you to capture the result of a function and use it elsewhere in your code.

returnValueFromFunction.js

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

let a = 5;
let b = 3;
let sum = add(5, 3);

console.log(`Sum of ${a} and ${b} is ${sum}`);

Output

Sum of 5 and 3 is 8

In this example,

1.   The add function takes two parameters, a and b.

2.   It returns the sum of a and b.

3.   The returned value is stored in the sum variable.

 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment