Search code examples
javascriptarrayscycleconsole.log

For...of : undefined log


I got this simple function, and I want to console out the values in an array:

function findRoutes(routes) {
    for (let value of routes) {
        console.log(value)
    } }


console.log(findRoutes([["MNL", "TAG"], ["CEB", "TAC"], ["TAG", "CEB"], ["TAC", "BOR"]]))

my console is:

["MNL", "TAG"]
["CEB", "TAC"]
["TAG", "CEB"]
["TAC", "BOR"]
undefined

What's that "undefined" at the end?


Solution

  • For Javascript,

    We know the functions returned value could be an object, an array, a function, a boolean, string and so on. But If the function has no return statement, undefined is returned. When you run any void function (like console.log) from the console, it also prints out info about the return value, undefined in this case.

    Read the top 2 voted answers here Chrome/Firefox console.log always appends a line saying undefined to clearly understand why you see the last undefined after your array?. I had same confusion few month back about the last undefined.