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?
For Javascript,
We know the functions returned value could be an
object
, anarray
, afunction
, aboolean
,string
and so on. But If the function has noreturn
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.