admin管理员组文章数量:1341450
In Chrome, the following
console.log(true, '\t');
will print
true " "
Why are there quotes hanging around?
(Notice that console.log(true + '', '\t')
will only print true
, in the same way that console.log('a', '\t');
will only print a
.)
In Chrome, the following
console.log(true, '\t');
will print
true " "
Why are there quotes hanging around?
(Notice that console.log(true + '', '\t')
will only print true
, in the same way that console.log('a', '\t');
will only print a
.)
- Not 100% sure, but my guess is because \t is a string expression representing a tab, chrome shows " " so you can distinguish that it is actually working. – Robert H Commented Aug 27, 2012 at 18:05
-
@asawyer: Doesn't happen for
console.log('a', '\t')
! – Randomblue Commented Aug 27, 2012 at 18:05 -
3
Note: It doesn't matter if you have
"\t"
or""
. As lobg as it is a string it will print it with quotes after the primitive valuetrue
. – some Commented Aug 27, 2012 at 18:10 -
1
i tried
console.log(true,'a\t')
it prints true "a ", butconsole.log('a\t',true)
prints a true(it has something to do with a previous boolean i think – Ankur Commented Aug 27, 2012 at 18:10 -
1
You keep editing the question. With
true + ""
you convert it to a sting, soconsole.log(true + '', '\t')
is the same asconsole.log(string,string)
. You can test the type if you dotypeof (true + '')
. – some Commented Aug 27, 2012 at 18:18
2 Answers
Reset to default 10Basically there are two overloads to console.log:
console.log(formatString, args)
and console.log(arg1, arg2, ...)
.
More specifically, per the source code, if the first parameter is a string then it treats it as a format string for the other parameters. Otherwise, each parameter is output directly.
Thus console.log(true + '', '\t')
outputs 'true' because the first parameter is a string and there is no placeholder for the \t
, and console.log(true, '\t')
will output both parameters because true
is not a string.
I decided to play around with it
console.log(true, '\t');
true " "
and then I tried the opposite
console.log(false, '\t');
false " "
Not sure why but false gives back only one space, while true gives back two o_O... Also if \t
is in the beginning there is no issue
console.log('\t', true);
true
It also doesn't matter what happens after it but it seems that the first parameter if its a boolean in general, will influence all the escaped tabs after with quotes.
console.log(false, '\t', '\t');
false " " " "
So it definitely has something to do with the first paramater being a boolean because if you try it with strings, it behaves pletely normally. I guess its a quirky thing with Google Chrome? I'll need to find the source code to actually see it.
本文标签: javascriptWhy does consolelog(true39t39) print true quotquotStack Overflow
版权声明:本文标题:javascript - Why does console.log(true, 't') print true ""? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743644745a2515316.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论