">

Convert JS to String

Convert JavaScript objects, functions, and multiline code into cleanly escaped strings, JSON strings, template literals, and single-line format.

JavaScript Code / Object Input
Converted String Representation

Why Convert JavaScript Code to a String?

JavaScript code frequently needs to be transmitted, stored, or dynamically executed in contexts where raw multiline scripts would violate syntax rules. Common real-world engineering scenarios include:

Output String Formats Explained

  1. JSON Double-Quoted: Uses standard JSON escape sequences (\" for quotes, \n for newlines). Ready to paste into any JSON property.
  2. Single-Quoted JS: Escapes single quotes (\') and newlines for embedding directly in JS scripts using single quotes.
  3. ES6 Template Literal: Backtick-enclosed string preserving original whitespace and escaping internal backticks (\`) and template expressions (\${...}).
  4. One-Line Minified: Strips excess leading/trailing whitespace and compresses consecutive spaces into a single line.
  5. URL-Encoded: Percent-encodes characters so code can be safely placed in query parameters or bookmarklets.
  6. Base64 Data URI: Converts code to Base64 (e.g. data:text/javascript;base64,...) for direct script tag insertion.

Frequently Asked Questions

How does "Evaluate Object → JSON.stringify()" work?
If you paste a JavaScript object literal with unquoted keys (like { name: "John", age: 30 }), standard JSON.parse fails. This option safely evaluates the object literal in browser memory and outputs formatted, strictly-valid JSON string.
Will this modify my original code logic?
No. Except for string escaping or whitespace compression, your underlying JavaScript tokens, variables, and logic remain untouched.