I would like to print an array line by line with carriage return.
Simple implementation is [1,2,3].forEach(function(x) {console.log(x)})
to get this as output
1
2
3
Now if I use the syntactic sugar of ES6 fat arrows,
michel$ node
> [1,2,3].forEach(x => console.log(x))
1
2
3
undefined
>
> [1,2,3].forEach(console.log)
1 0 [ 1, 2, 3 ]
2 1 [ 1, 2, 3 ]
3 2 [ 1, 2, 3 ]
undefined
When omitting the function parameter in the forEach
callback, it looks like the second version is returning a cartesian product of itself.
In other functional languages like Scala this is totally ok, why is this "wrong" in JavaScript?
michel$ scala
scala> Array(1,2,3).foreach(x => println(x))
1
2
3
scala> Array(1,2,3).foreach(println)
1
2
3