Skip to content

Template literals are lowered to + concatenation, calling valueOf instead of toString #1477

Closed

Description

@trackoor

Expected Behavior

Obfuscating an untagged template literal such as `Today: ${when}` should convert each substituted expression to a string using ToString (calling toString before valueOf, or passing the "string" hint to Symbol.toPrimitive), matching the runtime behavior of the original code (e.g. printing Today: Tue Nov 14 2023). Lowering template literals to ''.concat(...) preserves ES5 compatibility while calling ToString on each argument.

Current Behavior

JavaScriptObfuscator.obfuscate rewrites untagged template literals into '...' + expr binary expressions across all presets. For objects that define both valueOf and toString (such as Moment.js/Day.js instances or Decimal wrappers) or Symbol.toPrimitive, + invokes ToPrimitive with the "default" hint instead of ToString ("string" hint), so the obfuscated code calls valueOf instead of toString and prints Today: 1700000000000.

Steps to Reproduce

  1. Define an object with both valueOf and toString (or Symbol.toPrimitive) and interpolate it inside a template literal.
  2. Obfuscate the code with JavaScriptObfuscator.obfuscate(code).
  3. Execute both the original and obfuscated code and compare their output.

JavaScript Obfuscator Edition

  • JavaScript Obfuscator Open Source

Your Environment

  • Obfuscator version used: 5.10.0
  • Node version used: v24.14.1

Stack trace

N/A (wrong runtime output, no exception thrown)

Minimal working example that will help to reproduce issue

const JavaScriptObfuscator = require('javascript-obfuscator');

const code = `
var when = { valueOf() { return 1700000000000; }, toString() { return "Tue Nov 14 2023"; } };
console.log(\`Today: \${when}\`);
`;

eval(code);
// Output: Today: Tue Nov 14 2023

eval(JavaScriptObfuscator.obfuscate(code).getObfuscatedCode());
// Output: Today: 1700000000000

Credit: https://ucsbplse.github.io/

Activity

  1. added a commit that references this issue on Nov 11, 2026
  2. sanex3339 commented on Nov 12, 2026

    @sanex3339
    Member

    Released as 5.10.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions