Search code examples
javascriptarrow-functionsmap-function

invoking function inside console.log (returning still getting undefined)


javascript beginner here, i have two codes (one without function invoking and one with function invoking), first one is working as i want it here it is :

const numbers = [65, 44, 12, 4];
const newarray = numbers.map((myFunction)=>{
  return myFunction
})

console.log(newarray)

but second one i want to put everything inside a function and invoke that function in console.log, there seems to be some mistake in my code (have seen other similar but they did not return anything) ? i am returning but still getting 'undefined' here :

function app(){
const numbers = [65, 44, 12, 4];
const newarray = numbers.map((myFunction)=>{
  return myFunction
})}

console.log(app())


Solution

  • You just need to return at the end :)

    function app(){
        const numbers = [65, 44, 12, 4];
        const newarray = numbers.map((myFunction)=>{
            return myFunction;
        });
        return newarray;
    }
    
    console.log(app());


    (Answering comment)

    function App() {
        const numbers = [1, 2, 3, 4, 5];
        const listItems = numbers.map((number) => `<li>${number}</li>` );
        return `<ul>${listItems.join("")}</ul>`
    }
    console.log(App());