Kodeclik Blog
Can a Javascript function return multiple values?
Let us learn more about Javascript functions and how they work. We know Javascript functions can take multiple arguments. But can they return multiple values?
We will embed our Javascript code inside a HTML page like so:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Javascript function return values</title>
</head>
<body>
<script>
</script>
</body>
</html>
In the above HTML page you see the basic elements of the page like the head element containing the title (“Javascript function return values”) and a body with an empty script tag. The Javascript code goes inside these tags.
A basic Javascript function
Let us write a Javascript program to compute the number of permutations and the number of combinations of k elements chosen from a set of n elements. Recall that to find the number of permutations and combinations we need to use factorials. So, let us first write a factorial function and see if it works:
<script>
function fact(n) {
if (n == 0) {
return 1
} else {
return n * fact(n - 1)
}
}
document.write(fact(10))
</script>
Note that the factorial function is defined recursively. The last line in the script computes (and prints) the factorial of 10. The output is, as expected:
3628800
A Javascript function to compute permutations
Let us now write another function to compute the number of permutations of k elements chosen from a set of n elements.
<script>
function fact(n) {
if (n == 0) {
return 1
} else {
return n * fact(n - 1)
}
}
function permutations(n, k) {
return (fact(n) / fact(n - k))
}
document.write(fact(10))
document.write('<BR>')
document.write(permutations(5, 2))
</script>
The output will be:
3628800
20
A Javascript function to compute combinations
Now if we would like this function to also return the number of combinations of “n choose k”, we cannot quite do that. We will have to write a new function, like so:
<script>
function fact(n) {
if (n == 0) {
return 1
} else {
return n * fact(n - 1)
}
}
function permutations(n, k) {
return (fact(n) / fact(n - k))
}
function combinations(n,k) {
return (fact(n) / (fact(k) * fact(n - k)))
}
document.write(fact(10))
document.write('<BR>')
document.write(permutations(5, 2))
document.write('<BR>')
document.write(combinations(5, 2))
</script>
The output will be:
3628800
20
10
Packaging multiple return values from a function
If we desire to return multiple values, we must package them into an object like so:
<script>
function fact(n) {
if (n == 0) {
return 1
} else {
return n * fact(n - 1)
}
}
function combinatorics(n, k) {
permutations = fact(n) / fact(n - k)
combinations = fact(n) / (fact(k) * fact(n - k))
return {permutations, combinations}
}
document.write(fact(10))
document.write('<BR>')
answers = combinatorics(5, 2)
document.write('Permutations = ' + answers.permutations)
document.write('<BR>')
document.write('Combinations = ' + answers.combinations)
</script>
The output is:
3628800
Permutations = 20
Combinations = 10
As you can see from the above program, we now have a single function (called “combinatorics”) that computes both permutations and combinations. It then packages them into a single object and then returns it. In the routine where we call the combinatorics function, the results are stored in the “answers” variable. We then deconstruct the answers variable to find the permutations and combinations and print them separately.
So the moral of the story is: a function cannot technically return multiple values in Javascript but this is not a limitation in any real sense because you can always package your desired multiple values into a composite object and return the object for possible deconstruction later.
Interested in learning more Javascript? Learn how to loop through two Javascript arrays in coordination and how to scramble a string in Javascript!
Want to learn Javascript with us? Sign up for 1:1 or small group classes.