What does return inside return mean

Hello all, I was doing katas in codewars and was looking at a solution which was confusing to me as it has return inside of return

var number = function(array) {
  return array.map(function (line, index) {
    return (index + 1) + ": " + line;
  });
}

doesn’t it exist out of function when it hit return? If so then how comes it will iterate over the array?

this is the map method, the map method takes a function as argument and returns a new array

you can read more about it here:

your function returns another function, which returns a value on its own

I understand array,map, thank you but I want to know why there is return infront of array.map and then inside return as well.

array.map is a function and like most functions, it uses return to return some data

the return inside map is of the function that is argument of map, it is executed once for each element of the array and its output is used to build the array that map returns

number is a differnt function which also need a return statement, otherwise it returns undefined

not sure I understood.

Because the function returns the result of array.map. Functions resolve to some value, that array.map is going to produce an array, and that array is what is being returned.

functions is general return something after they run for example:

function function1() {
   console.log("Hello world!");
   return 0;
}

If i call this function using function1(), what it will do is that it will run whatever inside it and return 0 (return is like it will give a value after it finishes executing), for example if i do :

function function1() {
   console.log("Hello world!");
   return 0;
}
let theReturnedValue = function1()// theReturnedValue === 0

What will happen is function1() is going to run whatever inside it and after it finishes it will give out some value (or return some value), and theReturnedValue will be assigned the value returned by the function (i.e. 0)
but if the function was written without the return statement, the function will be considered undefined, for example:

function function1() {
   console.log("Hello world!");
}
let theReturnedValue = function1() // theReturnedValue === undefined

The map method returns a new array, (i.e. after it executes the code inside it it will give out a new array), so the function number, will return an array.

// This one will give an array ↓
array.map(function (line, index) {
    return (index + 1) + ": " + line;
  });
// and we will have the code look something like this ↓
var number = function(array) {
  return [/*the array produced by the map method*/]
}
// so now we can run the number function like that:
number([/*some array here*/])
1 Like

Thanks for explaining but I did not understand why there was a return infront of Array.map

the number function needs a return statement, otherwise it returns undefined

so I made this small example

const funk = ['hello','world',1,3]
const hello = () => {
   return funk.forEach(el => {
    return el;
  })
}
console.log(hello())

it returns undefined. I don’t understand why?

you have choosen the wrong method to try, forEach does not return anything

try with map, filter, reduce

also, is funk an array?

yes it is array, (sorry the format wasn’t on next line to show the value of funk

have you tried with a different method?

yes, it work fine now and I understand it. so basically if a function is inside a function, the inside function need to return, when it return, the outer function need to return the returned value from inside e.g

const funk = ['hello','world',1,3]
const hello = () => { //outside function
   return funk.forEach(el => {
    return el; //inside function
  })
}
console.log(hello())

am i right to think?

forEach still doesn’t return anything

also, somewhat

as map, filter, reduce, forEach and others are called “higher order functions”, which means they take a function as argument

each one do different things with that function

the two functions you see are not directly connected

undefined is what a function returns if it does not have a return statement, forEach is one of those that do not return, so they return undefined by default

see Understanding undefined value returned from a function

I don’t know who keeps flagging my previous comment as inappropriate, but

is not the same as:

Even if it doesn’t have a return statement it still returns undefined and not nothing.

‘The function does not have a return value so the default return value of undefined is used instead’ is a reasonable way to understand `forEach still doesn’t return anything’. @ieahleen’s explanation is correct.

I’m going to close this thread because this sort of linguistic pedantry is not useful or on topic.

2 Likes