To convert HTML to a JavaScript variable, serialize the exact HTML source as an escaped JavaScript string assigned to a const declaration.
Use the HTML to JavaScript Converter to paste an HTML fragment or full document, choose a conservative variable name and literal style, and copy one assignment. The generated source contains the escapes required by JavaScript; when that literal is evaluated, its string value should match the HTML text you supplied.
This workflow is for embedding markup as text. It is not a way to read an element from the current page, create a DOM, or turn HTML into executable component code.

What the generated assignment contains
The converter returns JavaScript source rather than a rendered page. For example, pasted HTML such as <section class="card"><h2>Welcome</h2></section> can become:
const html = "<section class=\"card\"><h2>Welcome</h2></section>";
The visible backslashes belong to the JavaScript source. They tell the parser that the following quotation mark is content rather than the end of the string. After evaluation, the variable contains the original section, attribute, and heading text.
- The output is one const assignment, not a collection of document.write() statements or DOM operations.
- Tags, attributes, comments, entities, inline styles, whitespace, and malformed markup remain literal text.
- The converter does not decode entities, reorder attributes, normalize capitalization, repair unclosed tags, or change the markup into JSX.
Convert HTML to a JavaScript variable
- Paste the HTML source exactly as it should appear in the final string. Copy the fragment or complete document without tidying, minifying, or otherwise changing it. This matters when whitespace, comments, entity text, or malformed tags are part of the value you need to preserve.
- Enter a non-reserved ASCII variable name. The name must begin with a letter, underscore, or dollar sign. Reserved words such as class, const, for, and return are rejected because they cannot serve as valid variable declarations.
- Choose double quotes, single quotes, or a template literal and convert. The selected mode determines how the generated string protects its delimiter and handles other syntax-sensitive characters.
- Copy the assignment and test it in isolation. Evaluate the copied literal in a small test, compare the resulting string with the untouched original, and only then integrate the assignment into your application.
The converter always emits const. If your application genuinely needs reassignment or a different module pattern, change the declaration manually and review the surrounding code rather than assuming the generated output is interchangeable with every JavaScript declaration style.
Select the right literal form
All three modes can represent HTML, but their readability and escaping behavior differ:
| Literal mode | Escaping behavior | Practical use |
|---|---|---|
| Double-quoted string | Escapes double quotes, backslashes, control characters, and line terminators; apostrophes remain readable. | A conventional choice when HTML attributes mainly use double quotes. |
| Single-quoted string | Escapes single quotes, backslashes, control characters, and line terminators; double quotes remain readable. | Useful when single-quoted attribute values would produce less escaping. |
| Template literal | Escapes backticks, ${, backslashes, control characters, and line separators while allowing readable multiline source. | Useful for multiline HTML, but the result remains an ordinary untagged template literal. |
A double-quoted result is compact and conventional. For example, the earlier section becomes const html = "<section class=\"card\"><h2>Welcome</h2></section>";. A template literal can present the same value across multiple lines without placing raw line terminators inside a conventional quoted string.
Template mode does not automatically create runtime interpolation. It produces a standard untagged template literal, and pasted ${ sequences are treated as content rather than executable expression syntax.
How escaping protects the generated source
Serialization and runtime interpretation are separate layers. Pasted HTML may contain a quotation mark that could terminate a JavaScript string, a backslash that could start an escape sequence, or a line terminator that is forbidden inside a normal string. The converter escapes these source-level hazards so the evaluated value resolves to the supplied text.
- Backslashes are escaped first. A value such as C:\temp therefore retains its literal backslash after evaluation instead of becoming an unintended escape sequence.
- The selected delimiter is protected. Double-quote mode escapes quotation marks, while single-quote mode escapes apostrophes.
- Line breaks are encoded. Newlines and carriage returns are represented with escapes rather than placed directly inside a normal JavaScript string.
- Portability-sensitive characters are handled. Common control characters and the Unicode line separators U+2028 and U+2029 are emitted explicitly instead of being left to break generated source.
Template literals have two additional hazards: a backtick can close the literal, and ${ can begin interpolation. For pasted content containing <p>${notJavaScript}</p>, template mode generates source equivalent to:
const html = `<p>\${notJavaScript}</p>`;
The runtime string contains the literal characters ${notJavaScript}, and no expression is evaluated. The generated template also escapes backslashes and control characters rather than relying on String.raw to solve every syntax concern.
These rules follow the ECMAScript string literal grammar, while the interpolation behavior of template literals is documented in MDN’s template literal reference.
Confirm an exact round trip
Copying the assignment is not the final verification step. Visually inspecting escaped output can make a mismatch difficult to spot, especially when the source contains mixed line endings, tabs, backslashes, or Unicode separators.
- Keep an untouched copy of the original HTML as the expected value in an isolated test.
- Evaluate the copied const assignment and compare it with the original using exact string equality.
- Do not trim, prettify, normalize entities, or otherwise transform either side before comparing them.
- If the values differ, recopy the source and check whether an editor or clipboard process changed whitespace or line endings.
A conceptual assertion is generatedHtml === originalHtml. This evaluates a string literal and compares text; it does not render or execute the markup. Never use eval or new Function merely to display HTML.
What the converter does not change
The tool performs text serialization rather than HTML parsing. That narrow behavior is deliberate because a formatter or parser can alter whitespace-sensitive content, scripts, styles, templates, and embedded data.
| Task | Supported behavior | Result |
|---|---|---|
| Store a pasted fragment or full document | Supported | Produces one copy-ready const assignment. |
| Preserve the supplied textual form | Supported | Tags, attributes, whitespace, comments, entities, styles, and malformed markup remain text. |
| Build a DOM | Not supported | No element nodes or parsed document are created. |
| Normalize or repair markup | Not supported | Attributes are not reordered, case is not normalized, and unclosed tags are not repaired. |
| Create JSX | Not supported | The result remains a JavaScript string assignment. |
| Validate or sanitize markup | Not supported | Malformed or unsafe markup is not rewritten into a trusted form. |
Use a maintained HTML parser when you need structural validation or transformation, and use a separate security process when the destination will render untrusted content.
Integrate the variable safely
A valid JavaScript assignment controls string syntax; it does not make the contained HTML safe. If the resulting string is later assigned to innerHTML, passed to a template engine, evaluated as code, or combined with untrusted data, the destination still needs context-appropriate escaping and sanitization.
- Prefer textContent when the destination does not need markup.
- Use a maintained sanitizer and a restrictive Content Security Policy when rendering untrusted markup is unavoidable.
- Keep pasted content separate from runtime template interpolation and review any expression added afterward.
- Never use eval or new Function merely to display HTML.
All processing takes place in the current browser tab. The pasted HTML is not uploaded, stored, executed, previewed, or sent to a server, and no account or new dependency is required. The result is a copy-ready source assignment that should still pass an exact round-trip check before it enters an application.
For a deeper look, see JavaScript Playground Example: Run a Complete Snippet.