In JavaScript, why does .forEach(console.log)
only give you the output you expect when you wrap console.log
in another function?
a = [0, 1, 2, 3, 4, 5, 6]
[ 0, 1, 2, 3, 4, 5, 6 ]
a.forEach(console.log)
0 0 [ 0, 1, 2, 3, 4, 5, 6 ] 1 1 [ 0, 1, 2, 3, 4, 5, 6 ] 2 2 [ 0, 1, 2, 3, 4, 5, 6 ] 3 3 [ 0, 1, 2, 3, 4, 5, 6 ] 4 4 [ 0, 1, 2, 3, 4, 5, 6 ] 5 5 [ 0, 1, 2, 3, 4, 5, 6 ] 6 6 [ 0, 1, 2, 3, 4, 5, 6 ] undefined
a.forEach(function(n) { console.log(n) })
0 1 2 3 4 5 6 undefined
What causes this difference in output?