HTML Encoder/Decoder for JavaScript

Free online html encoder/decoder with JavaScript code examples

Working with html encoder/decoder in JavaScript? Our free online html encoder/decoder helps JavaScript developers format, validate, and process data instantly. Below you will find JavaScript code examples using he / DOM API so you can achieve the same result programmatically in your own projects.

Try the HTML Encoder/Decoder Online

Use our free HTML Encoder/Decoder directly in your browser — no setup required.

Open HTML Encoder/Decoder

JavaScript Code Example

// Using the 'he' library (Node.js)
const he = require('he');

const encoded = he.encode('<script>alert("XSS")</script>');
console.log(encoded); // &lt;script&gt;alert(&quot;XSS&quot;)&lt;/script&gt;

const decoded = he.decode(encoded);
console.log(decoded);

// Browser alternative:
// const div = document.createElement('div');
// div.textContent = text; encoded = div.innerHTML;

Quick Setup

Library: he / DOM API
npm install he

JavaScript Tips & Best Practices

  • In browsers, use textContent/innerHTML for encoding
  • The 'he' library handles all HTML entities correctly
  • Always encode user input to prevent XSS attacks

Frequently Asked Questions

HTML Encoder/Decoder in Other Languages

More JavaScript Tools