Skip to content

console.log({ '"\'': 0 }) gives syntactically invalid JSΒ #57024

@lionel-rowe

Description

@lionel-rowe

Version

23.3.0

Platform

Linux DESKTOP-3S4OV0F 5.15.167.4-microsoft-standard-WSL2 #1 SMP Tue Nov 5 00:21:55 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

Subsystem

No response

What steps will reproduce the bug?

console.log({ '"\'': 0 })
// logs { `"'`: 0 }

However, { `"'`: 0 } is not syntactically valid in JS:

let x
x = { `"'`: 0 }
// parse error: Unexpected token ```. Expected identifier, string literal, numeric literal or [ for the computed key at 2:7
x = { '"\'': 0 }
// ok
x = { [`"'`]: 0 }
// ok

While there's no hard requirement for console.log to always yield syntactically invalid JS (e.g. objects such as File { name: "", size: 0, type: "" }, truncation with ..., etc.) I'd generally expect small JSON-compatible objects to be displayed in a way I can directly copy-paste into my code.

For comparison, Deno has the same issue, while Bun doesn't.

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

Always, given an object with a key that contains both single ' and double " quotes.

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

Either { '"\'': 0 }, { "\"'": 0 }, or { [`"'`]: 0 }

What do you see instead?

{ `"'`: 0 }

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    consoleIssues and PRs related to the console subsystem.questionIssues that look for answers.utilIssues and PRs related to the built-in util module.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions