# How To Return Two Values In The Same Function

**URL:** <https://forum.freecodecamp.org/t/how-to-return-two-values-in-the-same-function/401211>\
**Category:** General\
**Created:** [June 9, 2020, 4:58pm UTC](https://forum.freecodecamp.org/t/how-to-return-two-values-in-the-same-function/401211 "2020-06-09T16:58:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ErcDz](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ercdz/32/296903_2.png) [@ErcDz](https://forum.freecodecamp.org/u/ErcDz)\
**Post date:** [June 9, 2020, 4:58pm UTC](https://forum.freecodecamp.org/t/how-to-return-two-values-in-the-same-function/401211/1 "2020-06-09T16:58:30Z")

</div>

I was wondering how I could return these two values (totalPositive and newArr) in the same function. I want to return the array and then modify it (add all the values together and return that value). Here’s my code:

```auto
function sumDigits(num) {
  var newArr = []
  var stringifiedNum = num.toString()
  for (var i = 0; i < stringifiedNum.length; i++) {
    newArr.push(stringifiedNum[i])
  }

return newArr

var totalPositive = 0
for (var i = 0; i < newArr.length; i++) {
  totalPositive += newArr[i]
}
// return totalPositive?
 }

var outputPositive = sumDigits(1148);
console.log(outputPositive); // --> 14

```

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [June 9, 2020, 5:01pm UTC](https://forum.freecodecamp.org/t/how-to-return-two-values-in-the-same-function/401211/2 "2020-06-09T17:01:02Z")

</div>

If you want to pass out two values, then you need to pass them out in an array.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 9, 2020, 5:05pm UTC](https://forum.freecodecamp.org/t/how-to-return-two-values-in-the-same-function/401211/3 "2020-06-09T17:05:05Z")

</div>

also remember that once a return value is executed the function stops, so anything you put after a return statement, it is not reached

---

<div class="post-metadata">

**Author:** ![lasjorg](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lasjorg/32/280761_2.png) [@lasjorg](https://forum.freecodecamp.org/u/lasjorg)\
**Post date:** [June 9, 2020, 5:13pm UTC](https://forum.freecodecamp.org/t/how-to-return-two-values-in-the-same-function/401211/4 "2020-06-09T17:13:50Z")

</div>

Using an object is a fairly common way to return multiple values out of a function. But as said, an array would work as well.

```auto
function addAndSubtract(num1, num2) {

  let addition = num1 + num2;
  let subtraction = num1 - num2;

  return {
    addition,
    subtraction,
  };
}

const resObj = addAndSubtract(2,2)
const { addition, subtraction } = resObj

console.log(resObj); // { addition: 4, subtraction: 0 }
console.log(addition); // 4
console.log(subtraction); // 0

```

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 6:50pm UTC](https://forum.freecodecamp.org/t/how-to-return-two-values-in-the-same-function/401211/5 "2021-06-01T18:50:20Z")

</div>

Closed on June 1, 2021, 6:50pm UTC


