In nodeJs,
console.log("before ", "\n", "after")
I expect the output,
before
after
But it prints,
before
after
In nodeJs,
console.log("before ", "\n", "after")
I expect the output,
before
after
But it prints,
before
after
Share
Improve this question
edited Dec 7, 2015 at 15:55
asked Dec 7, 2015 at 15:52
user2879704user2879704
5
- Not in my browser (Firebug's console prints everything on one line). What's your question, by the way? – Frédéric Hamidi Commented Dec 7, 2015 at 15:53
-
5
console.log
behavior is not standard. In chrome, multiple arguments are all printed in one line. Some other browsers will treat each argument in a different line. – MinusFour Commented Dec 7, 2015 at 15:54 - @MinusFour Wich browser for example? I tried with Iron (chromium), Firefox, Internet Explorer, Edge and they all print in one line. – Shanoor Commented Dec 7, 2015 at 16:02
- @ShanShan I can't say which ones do that. I guess what I tried to say is that other browsers can print each argument in a separate line as there is no standard that they need to adhere to. – MinusFour Commented Dec 7, 2015 at 16:05
- @MinusFour I see. It's not standard but today, I think the behaviour is consistent across the most used javascript engines. – Shanoor Commented Dec 7, 2015 at 16:09
5 Answers
Reset to default 4The reason you get these spaces " " in your Browser, is that it will add a space-symbol inbetween every argument:
console.log("before ", "\n", "after")
will be like:
"before \n after"
To avoid these spaces do string concatenation by your own:
console.log("before " + "\n" + "after")
Also if you want a blank line inbetween use two \n
:
console.log("before " + "\n\n" + "after")
And remember Console.log()
is a non-standard feature, a stated here:
Non-standard This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large inpatibilities between implementations and the behavior may change in the future.
\n
means one line feed. If you want two line feed you should use \n\n
. Like this
console.log("before ", "\n\n", "after")
If your objective is to put the output on different lines, you'll want to use string concatenation. To achieve the output you would like to see, try the following:
console.log('before' + '\n\n' + 'after');
The result will be:
before
after
If you want end result
before
after
Then Try
console.log("before\n\nafter");
The issue is observed in Nodejs mand line execution. You just need to change your code-
console.log("before\nafter");