About
Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.
Popular Classes
Scratch Coding
Minecraft Coding
TinkerCAD
Roblox Studio
Python for Kids
Javascript for Kids
Pre-Algebra
Geometry for Kids
Copyright @ Kodeclik 2025. All rights reserved.
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:
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.
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:
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:
Let us now write another function to compute the number of permutations of k elements chosen from a set of n elements.
The output will be:
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:
The output will be:
If we desire to return multiple values, we must package them into an object like so:
The output is:
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.
<!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>
<script>
function fact(n) {
if (n == 0) {
return 1
} else {
return n * fact(n - 1)
}
}
document.write(fact(10))
</script>
3628800
<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>
3628800
20
<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>
3628800
20
10
<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>
3628800
Permutations = 20
Combinations = 10