Javascript return statements

pls enlightened me about this return statement :

 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

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

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.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

ok sir next time sorry my first time hehehehe

1 Like

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?

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

thearg.map(function(element) {

        return multiplier * element;

    });

}

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

console.log(arr);

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.

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

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.!

so your function is

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

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.

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

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?

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

let’s write it like this

function multiply(multiplier, ...thearg) {

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

1 Like

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;

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…