Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does console.log add a space at the end of my sentence, when adding a value with the comma operator?

Tags:

javascript

This question is almost not worth asking, but here I am.

Referencing the question above, here is the code and output

var port = 3000

console.log("Listening on port ", port)

Outputs "Listening on port 3000"

Notice the extra space thrown in there. Why does this occur?

like image 876
Keith Grout Avatar asked Feb 13 '23 02:02

Keith Grout


1 Answers

By popular demand, copied from comments:

Because that's what console.log does. Might as well ask why it prints on the console instead of painting your bathroom blue. (Because the latter is not what it does). :) If you don't want the space, use + operator to stitch strings together.

The reason is, likely, the fact that console.log, at least in graphical clients, has the ability to present objects and arrays inline. This makes the displayed result not really a string, but more of a table, with the space separating each cell from another. Each argument is, thus, presented separately.

Also, considering that console.log is used for debugging, console.log(i, j, a[i][j], a[i][j] / 2) showing 3724.712.35 is not really all that useful, when compared to 3 7 24.7 12.35. And writing console.annoying_log(i + ' ' + j + ' ' + a[i][j] + ' ' + a[i][j] / 2) is annoying. I had enough of that from Java, when I was younger.

like image 167
Amadan Avatar answered Feb 14 '23 17:02

Amadan