# Javascript return statements

**URL:** <https://forum.freecodecamp.org/t/javascript-return-statements/482279>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [October 19, 2021, 6:09am UTC](https://forum.freecodecamp.org/t/javascript-return-statements/482279 "2021-10-19T06:09:50Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![altabz](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/altabz/32/183442_2.png) [@altabz](https://forum.freecodecamp.org/u/altabz)\
**Post date:** [October 19, 2021, 6:09am UTC](https://forum.freecodecamp.org/t/javascript-return-statements/482279/1 "2021-10-19T06:09:50Z")

</div>

pls enlightened me about this return statement :

```auto
 return thearg.map(function(element) {
        return multiplier * element;         

```

my quesstion is that what is the purpose of returning the map method result ?? the purpose of the function is to multiply values and return the result the caller only require the result not the array thanks

---

<div class="post-metadata">

**Author:** ![bbsmooth](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bbsmooth/32/203556_2.png) [@bbsmooth](https://forum.freecodecamp.org/u/bbsmooth)\
**Post date:** [October 19, 2021, 6:14am UTC](https://forum.freecodecamp.org/t/javascript-return-statements/482279/2 "2021-10-19T06:14:10Z")

</div>

Can you wrap the actual code in triple backticks so it formats properly?

---

<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:** [October 19, 2021, 6:19am UTC](https://forum.freecodecamp.org/t/javascript-return-statements/482279/3 "2021-10-19T06:19:46Z")

</div>

The map method returns a new array, if it is being used in a function, you need to do something with the returned array.

* * *

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (`</>`) to add backticks around text.

 ![Pre-formatted-text](https://global.discourse-cdn.com/freecodecamp/original/3X/1/b/1b18854f1059217f29370444396145cc3a0cd8ae.gif)

See [this post](https://forum.freecodecamp.org/t/forum-code-formatting/25574) to find the backtick on your keyboard.  
**Note:** Backticks (`) are not single quotes (’).

---

<div class="post-metadata">

**Author:** ![altabz](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/altabz/32/183442_2.png) [@altabz](https://forum.freecodecamp.org/u/altabz)\
**Post date:** [October 19, 2021, 6:52am UTC](https://forum.freecodecamp.org/t/javascript-return-statements/482279/4 "2021-10-19T06:52:43Z")

</div>

ok sir next time sorry my first time hehehehe

---

<div class="post-metadata">

**Author:** ![altabz](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/altabz/32/183442_2.png) [@altabz](https://forum.freecodecamp.org/u/altabz)\
**Post date:** [October 19, 2021, 7:19am UTC](https://forum.freecodecamp.org/t/javascript-return-statements/482279/5 "2021-10-19T07:19:52Z")

</div>

thanks much my first time…follow up question is why is it that if i don’t return the map method the result of the function call is undefined even if i return the result that is required?

---

<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:** [October 19, 2021, 7:26am UTC](https://forum.freecodecamp.org/t/javascript-return-statements/482279/6 "2021-10-19T07:26:02Z")

</div>

I don’t think I am following, do you have the code you are talking about?

---

<div class="post-metadata">

**Author:** ![altabz](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/altabz/32/183442_2.png) [@altabz](https://forum.freecodecamp.org/u/altabz)\
**Post date:** [October 19, 2021, 7:28am UTC](https://forum.freecodecamp.org/t/javascript-return-statements/482279/7 "2021-10-19T07:28:32Z")

</div>

```auto
thearg.map(function(element) {

        return multiplier * element;

    });

}

var arr = multiply(2, 1, 1, 3);

console.log(arr);

```

---

<div class="post-metadata">

**Author:** ![altabz](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/altabz/32/183442_2.png) [@altabz](https://forum.freecodecamp.org/u/altabz)\
**Post date:** [October 19, 2021, 7:31am UTC](https://forum.freecodecamp.org/t/javascript-return-statements/482279/8 "2021-10-19T07:31:44Z")

</div>

this function returns a undefined i removed the return statement in the map method because i don’t need the array only the result of multiplier \* element.

---

<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:** [October 19, 2021, 7:32am UTC](https://forum.freecodecamp.org/t/javascript-return-statements/482279/9 "2021-10-19T07:32:52Z")

</div>

where is `multiply` defined? I think there is something missing there

---

<div class="post-metadata">

**Author:** ![altabz](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/altabz/32/183442_2.png) [@altabz](https://forum.freecodecamp.org/u/altabz)\
**Post date:** [October 19, 2021, 7:42am UTC](https://forum.freecodecamp.org/t/javascript-return-statements/482279/10 "2021-10-19T07:42:46Z")

</div>

```auto
function multiply(multiplier, ...thearg) 

```

the name of the function is multiply that is the statement above i post it maps the array, the first element of the array is the multiplier of the preceding values.!

---

<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:** [October 19, 2021, 7:51am UTC](https://forum.freecodecamp.org/t/javascript-return-statements/482279/11 "2021-10-19T07:51:20Z")

</div>

so your function is

```auto
function multiply(multiplier, ...thearg) {
  return thearg.map(function(element) {
    return multiplier * element;
  });
}
var arr = multiply(2, 1, 1, 3);
console.log(arr);

```

what’s your question about it? now that I have seen all the function I may be able to answer

---

<div class="post-metadata">

**Author:** ![altabz](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/altabz/32/183442_2.png) [@altabz](https://forum.freecodecamp.org/u/altabz)\
**Post date:** [October 19, 2021, 7:58am UTC](https://forum.freecodecamp.org/t/javascript-return-statements/482279/12 "2021-10-19T07:58:58Z")

</div>

I try to remove the return statement in the thearg.map what happen is that the return value become undefined so i wonder… the caller only require the result of the multiplier \* element … so why is it that when i removed the return statement in the thearg.map the return value is undefined? it goes like this `thearg.map(function(element)){ return multiplier * element;}); }` the output is undefined.

---

<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:** [October 19, 2021, 8:17am UTC](https://forum.freecodecamp.org/t/javascript-return-statements/482279/13 "2021-10-19T08:17:01Z")

</div>

if you remove the `return` from inside `multiply` then `multiply` doesn’t have a return statement anymore, and if a function doesn’t have a return statement it returns `undefined` by default

---

<div class="post-metadata">

**Author:** ![altabz](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/altabz/32/183442_2.png) [@altabz](https://forum.freecodecamp.org/u/altabz)\
**Post date:** [October 19, 2021, 8:21am UTC](https://forum.freecodecamp.org/t/javascript-return-statements/482279/14 "2021-10-19T08:21:10Z")

</div>

but i return the product of multiplier and the element is that not enough? `return multiplier * element;` is this not enough ? the caller only require the the product of multiplier and element and not the whole array itself?

---

<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:** [October 19, 2021, 8:22am UTC](https://forum.freecodecamp.org/t/javascript-return-statements/482279/15 "2021-10-19T08:22:42Z")

</div>

inside `map` there is a function, which is a different function than `multiply`, with its own return statement. `multiply` doesn’t have a return statement

---

<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:** [October 19, 2021, 8:23am UTC](https://forum.freecodecamp.org/t/javascript-return-statements/482279/16 "2021-10-19T08:23:52Z")

</div>

let’s write it like this

```auto
function multiply(multiplier, ...thearg) {

  function myFunction(element) {
    return multiplier * element;
  }
  return thearg.map(myFunc);
}
var arr = multiply(2, 1, 1, 3);
console.log(arr);

```

---

<div class="post-metadata">

**Author:** ![altabz](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/altabz/32/183442_2.png) [@altabz](https://forum.freecodecamp.org/u/altabz)\
**Post date:** [October 19, 2021, 8:44am UTC](https://forum.freecodecamp.org/t/javascript-return-statements/482279/18 "2021-10-19T08:44:56Z")

</div>

thank you very much.! what you mean to say is that the return statement of multiply function is also a function `return thearg.map(function(element)` and inside this function the values also needed to be return ` return multiplier * element;`

---

<div class="post-metadata">

**Author:** ![altabz](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/altabz/32/183442_2.png) [@altabz](https://forum.freecodecamp.org/u/altabz)\
**Post date:** [October 19, 2021, 8:48am UTC](https://forum.freecodecamp.org/t/javascript-return-statements/482279/19 "2021-10-19T08:48:55Z")

</div>

i also try to remove the return statement inside the map method ` return thearg.map(function(element) { multiplier * element;` the output is [undefined,undefined,undefined] i thought is that if your return the map function the values will go with it but i realize javascript is not negotiable you need to return everything thank so much for the patience … in answering my questions…

---

<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:** [August 12, 2022, 2:32pm UTC](https://forum.freecodecamp.org/t/javascript-return-statements/482279/20 "2022-08-12T14:32:09Z")

</div>

Closed on August 12, 2022, 2:32pm UTC


