I am running these examples of Node.js v10.5.0. When I only print strings with it, it prints the strings without surrounding quotes.
> console.log('foo', 'bar')
foo bar
undefined
But when I print strings and numbers together, then it prints strings with surrounding quotes.
> console.log(1, 'foo', 'bar')
1 'foo' 'bar'
undefined
Why does this difference occur? I was expecting it to print the following in the second example:
1 foo bar
Similar behavior can be observed with Chrome 70.
It looks like console.log()
chooses to show string in quotes when there are arguments of number type but then these examples print all strings without quotes even when numbers are involved:
> console.log('foo', 'bar', 1)
foo bar 1
undefined
> console.log('foo', 1, 'bar')
foo 1 bar
undefined
What's going on here? Why does console.log()
print strings with quotes in some cases and not in other cases?
I am running these examples of Node.js v10.5.0. When I only print strings with it, it prints the strings without surrounding quotes.
> console.log('foo', 'bar')
foo bar
undefined
But when I print strings and numbers together, then it prints strings with surrounding quotes.
> console.log(1, 'foo', 'bar')
1 'foo' 'bar'
undefined
Why does this difference occur? I was expecting it to print the following in the second example:
1 foo bar
Similar behavior can be observed with Chrome 70.
It looks like console.log()
chooses to show string in quotes when there are arguments of number type but then these examples print all strings without quotes even when numbers are involved:
> console.log('foo', 'bar', 1)
foo bar 1
undefined
> console.log('foo', 1, 'bar')
foo 1 bar
undefined
What's going on here? Why does console.log()
print strings with quotes in some cases and not in other cases?
-
You wouldn't know if
1
was string or number then would you? Note different browsers also handle this differently – charlietfl Commented Dec 12, 2018 at 16:08 -
@charlietfl I wouldn't in that case. That's right. But if I wanted to know the type, I could do something like
console.log(1, typeof 1, 'foo', typeof 'foo')
. Is itconsole.log()
's responsibility to show me the types by default? – Lone Learner Commented Dec 12, 2018 at 16:09 - is probably doing that out of convenience when you pass in different types – charlietfl Commented Dec 12, 2018 at 16:10
-
It seems to me that in the specific case where all arguments are strings,
console.log
outputs the concatenated, unquoted string. If any argument is not a string, you get individual quoted strings. – Niet the Dark Absol Commented Dec 12, 2018 at 16:10 -
@NiettheDarkAbsol I thought so but here is a counterexample:
console.log('foo', 'bar', 1)
. This printsfoo bar 1
(without any quotes). – Lone Learner Commented Dec 12, 2018 at 16:13
2 Answers
Reset to default 3There's not a legitimate standard yet for console.log
but most browsers, including Chrome (i.e. Chromium), use the working group specification (WHATWG):
https://console.spec.whatwg/#logger
According to this spec, if you have differing numbers of parameters then different methods are used to output the data, according to the current specification:
2.1 Logger(logLevel, args)
If args is empty, return.
Let first be
args[0]
.Let rest be all elements following first in args.
If rest is empty, perform
Printer(logLevel, « first »)
and return.If first does not contain any format specifiers, perform
Printer(logLevel, args)
.Otherwise, perform
Printer(logLevel, Formatter(args))
.Return
undefined
.
Ultimately, the number of parameters determine the methods that are used to output information. Specifically, in your example the first parameter can not contain a format specifier because it's a number so it gets passed to Printer() but if the first parameter is a string then it gets passed to Formatter().
So, you get different output depending on order:
> console.log('hello',1,'hello')
hello 1 hello
versus
> console.log(1,'hello','hello')
1 "hello" "hello"
Ultimately, how those methods output information is implementation/browser dependent:
How the implementation prints args is up to the implementation, but implementations should separate the objects by a space or something similar, as that has bee a developer expectation.
This seems like a deliberate choice by the Chrome team. There is no standard for how console.log works in different environments.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744390955a4571902.html
评论列表(0条)