Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does console.log(true, '\t') print true " "?

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.)

like image 224
Randomblue Avatar asked Aug 27 '12 18:08

Randomblue


People also ask

Is console log and print the same?

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.

Does console log mean print?

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.

Does console log print a new line?

By default, the console. log() method prints on console with trailing newline.

What is the output of console log Boolean?

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.


2 Answers

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.

like image 141
Steve Campbell Avatar answered Nov 15 '22 07:11

Steve Campbell


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.

like image 28
aug Avatar answered Nov 15 '22 07:11

aug