javascript - Why does console.log log strings with quotes in some cases? - Stack Overflow

I am running these examples of Node.js v10.5.0. When I only print strings with it, it prints the string

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?

Share Improve this question edited Dec 12, 2018 at 16:14 Lone Learner asked Dec 12, 2018 at 16:03 Lone LearnerLone Learner 20.8k25 gold badges114 silver badges220 bronze badges 7
  • 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 it console.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 prints foo bar 1 (without any quotes). – Lone Learner Commented Dec 12, 2018 at 16:13
 |  Show 2 more ments

2 Answers 2

Reset to default 3

There'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)

  1. If args is empty, return.

  2. Let first be args[0].

  3. Let rest be all elements following first in args.

  4. If rest is empty, perform Printer(logLevel, « first ») and return.

  5. If first does not contain any format specifiers, perform Printer(logLevel, args).

  6. Otherwise, perform Printer(logLevel, Formatter(args)).

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

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信