Undefined log to console in for loop

I keep getting an undefined log to my console, it seems to be a pattern when I am using for loops , not sure if I have looped wrong, too far ect. Can you see anything from this?

const aliens = ["Blorgous", "Glamyx", "Wegord", "SpaceKing"]; function greetAliens(ar) { for (let i = 0; i < ar.length; i++) { console.log(`Oh powerful ${ar[i]}, we humans offer our unconditional surrender!`); } }; console.log(greetAliens(aliens));

Hello @willplattt, welcome to the forums! Nothing’s technically wrong; the undefined comes from how you’re calling the function. Since your greetAliens function doesn’t explicitly return anything (as in, you don’t have a return statement in it), JS defaults to undefined. Essentially, your function returns undefined. Therefore, when you log the function call:

console.log(greetAliens(aliens));

Not only do the console.log() statements within the function run, but the return value (undefined) also gets logged.

3 Likes

Thankyou for the help with this!

1 Like