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
.)
Here's a quick lesson for you: In your web browser's devtools, your console. log() does not capture the objects you print when you print them. It captures it when you look at the printout.
The console. log() is a function in JavaScript that is used to print any kind of variables defined before in it or to just print any message that needs to be displayed to the user.
By default, the console. log() method prints on console with trailing newline.
So by this logic console. log() prints first truthy expression in your statement. If you were to try console. log(null || 2) , then 2 would be printed out.
Basically 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 completely normally. I guess its a quirky thing with Google Chrome? I'll need to find the source code to actually see it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With