Skip to content

console.log("%s", { [Symbol.toPrimitive]: () => "hello" }) shows the object, not "hello"Β #50909

@jcbhmr

Description

@jcbhmr

Version

v21.2.0

Platform

Linux PIG-2016 5.15.90.1-microsoft-standard-WSL2 #1 SMP Fri Jan 27 02:56:13 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

Subsystem

inspect

What steps will reproduce the bug?

jcbhmr@PIG-2016:~$ node
Welcome to Node.js v21.2.0.
Type ".help" for more information.
> let o; o = { __proto__: null, [Symbol.toPrimitive]: () => "hello" }
[Object: null prototype] {
  [Symbol(Symbol.toPrimitive)]: [Function: [Symbol.toPrimitive]]
}
> console.log("%s", o)
[Object: null prototype] {
  [Symbol(Symbol.toPrimitive)]: [Function: [Symbol.toPrimitive]]
}
undefined
>

How often does it reproduce? Is there a required condition?

always

What is the expected behavior? Why is that the expected behavior?

deno does:

jcbhmr@PIG-2016:~$ deno
Deno 1.38.3
exit using ctrl+d, ctrl+c, or close()
REPL is running with all permissions allowed.
To specify permissions, run `deno repl` with allow flags.
> let o; o = { __proto__: null, [Symbol.toPrimitive]: () => "hello" }
[Object: null prototype] {
  [Symbol(Symbol.toPrimitive)]: [Function: [Symbol.toPrimitive]]
}
> console.log("%s", o)
hello
undefined
>

chrome browser does:

< let o; o = { __proto__: null, [Symbol.toPrimitive]: () => "hello" }
> {Symbol(Symbol.toPrimitive): Ζ’}
< console.log("%s", o)
VM5744:1 hello
> undefined

bun does:

jcbhmr@PIG-2016:~$ bun repl
Welcome to Bun v1.0.14
Type ".help" for more information.
[!] Please note that the REPL implementation is still experimental!
    Don't consider it to be representative of the stability or behavior of Bun overall.
> let o; o = { __proto__: null, [Symbol.toPrimitive]: () => "hello" }
[Object: null prototype] {
  [Symbol(Symbol.toPrimitive)]: [Function: [Symbol.toPrimitive]]
}
> console.log("%s", o)
hello
undefined
>

firefox browser does:

>> let o; o = { __proto__: null, [Symbol.toPrimitive]: () => "hello" }
<- Object { Symbol("Symbol.toPrimitive"): Symbol.toPrimitive() }

>> console.log("%s", o)
hello                                          debugger eval code:1:9
<- undefined

makes sense; it has a stringifier so it should act just like it would if it had a toString() which Node.js does recognize:

jcbhmr@PIG-2016:~$ node
Welcome to Node.js v21.2.0.
Type ".help" for more information.
> let o; o = { __proto__: null, toString: () => "hello" }
[Object: null prototype] { toString: [Function: toString] }
> console.log("%s", o)
hello
undefined
>

What do you see instead?

jcbhmr@PIG-2016:~$ node
Welcome to Node.js v21.2.0.
Type ".help" for more information.
> let o; o = { __proto__: null, [Symbol.toPrimitive]: () => "hello" }
[Object: null prototype] {
  [Symbol(Symbol.toPrimitive)]: [Function: [Symbol.toPrimitive]]
}
> console.log("%s", o)
[Object: null prototype] {
  [Symbol(Symbol.toPrimitive)]: [Function: [Symbol.toPrimitive]]
}
undefined
>

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugIssues with confirmed bugs.consoleIssues and PRs related to the console subsystem.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions