【问题标题】:why is there a space after console logging with a backslash n?为什么控制台记录后有一个空格,带有反斜杠 n?
【发布时间】:2023-03-08 00:14:01
【问题描述】:

换行符之后的内容似乎以一个空格的缩进开头。

console.log('foo\n', 'bar'); 

此日志:

foo
 bar

为什么 bar 不在 foo 的正下方?您将如何记录从该行的同一点开始的文本?

【问题讨论】:

  • 没有\n的输出是什么样子的?
  • 换行和/或一个或多个空格只会在 HTML 中创建一个空格。

标签: javascript newline


【解决方案1】:

当您使用console.log('foo', 'bar'); 时,它会自动在这两个日志之间添加一个空格。如果你想排除这个空间,就用这个:

console.log('foo', '\nbar'); 

它将解释为没有空格的新行。

【讨论】:

  • 更准确地说,是一个空格,然后是一个换行符......所以只是一个尾随空格。
【解决方案2】:

这是根据行为。
在 JS 中,如果你给 console.log() 一个逗号分隔的列表,所有这些列表都会被空格分隔。

console.log('foo','bar'); 
//foo bar

您可以使用以下任一选项:

console.log('foo','\nbar');

//OR if the above looks too ugly for your taste,

console.log('foo');
console.log('bar');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-02
    • 1970-01-01
    • 2021-05-21
    • 1970-01-01
    • 2011-01-23
    相关资源
    最近更新 更多