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*/])