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
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*/])
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?
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
‘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’. @ILM’s explanation is correct.
I’m going to close this thread because this sort of linguistic pedantry is not useful or on topic.