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?
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?
Share Improve this question asked Sep 4, 2014 at 6:21 Keith GroutKeith Grout 9091 gold badge11 silver badges32 bronze badges 3-
3
Remove the space from the string,
console.log
already does that for you. – elclanrs Commented Sep 4, 2014 at 6:22 -
2
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. – Amadan Commented Sep 4, 2014 at 6:26 -
1
@Amadan Lol fair enough. This is actually the answer I was looking for. I understand the difference between
,
and+
, and I wasn't looking for how to correct it. Only why it does that. Thanks to other answerers though. – Keith Grout Commented Sep 4, 2014 at 6:33
3 Answers
Reset to default 8By popular demand, copied from ments:
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 pared 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.
From https://developer.mozilla/en-US/docs/Web/API/console.log the method log
takes as arguments a list of Javascript objects
console.log(obj1 [, obj2, ..., objN);
You're right in the documentation it says that the objects are appended together but it doesn't say that the separator is a single space.
obj1 ... objN A list of JavaScript objects to output. The string representations of each of these objects are appended together in the order listed and output.
Should be be doing
console.log("Listening on port " + port)
As (I think) it might be treating the arguments as an array and thus join it together with a default delimiter being a space