_.unescape() reverses HTML entity encoding—turning sequences like & and < back into & and <. It pairs with _.escape() for round-trip encode/decode workflows when storing or transporting text.
💡
Beginner tip
Think of _.unescape(stored) as “decode what _.escape() produced”—but always validate trust level before rendering HTML.
Foundation
📝 Syntax
javascript
_.unescape(string)
javascript
import unescape from "lodash/unescape";
const plain = unescape("Fast & reliable <widget>");
// -> "Fast & reliable <widget>"
_.unescape() restores literals from HTML entities—essential when reversing _.escape() output. Treat security seriously: decode only in trusted pipelines and prefer textContent for user-generated text.
Pair unescape with escape for symmetric storage pipelines
Use textContent when displaying decoded user text
Sanitize before innerHTML even after unescape
Test named entities your CMS actually emits
Import lodash/unescape per method
❌ Don’t
Assign unescaped user strings directly to innerHTML
Assume all HTML5 entities are supported
Use unescape as an XSS fix—it exposes markup
Decode twice without checking source trust
Skip escape on output after unescaping untrusted input
Summary
Key Takeaways
📄01
Decode entities
& → &
Core
📄02
vs escape
Encode/decode pair
Pattern
📄03
Security
Sanitize output
Critical
📄04
Named entities
♥ ♣ ♦ ♠
Feature
📄05
upperCase
Next method
Nav
❓ Frequently Asked Questions
_.unescape() converts HTML entities in a string back to their original characters—for example & becomes & and < becomes <.
Yes. _.escape() converts &, <, >, ", and ' to HTML entities. _.unescape() reverses those entities and a few named entities like &hearts;.
Unescaping can expose HTML. Sanitize or escape on output. Never assign unescaped user strings to innerHTML without a trusted sanitization step.
It handles the entities Lodash documents—common named entities and numeric forms. For full HTML5 entity tables, consider a dedicated decoder.
When displaying content that was escaped for storage or transport, or when reversing _.escape() output before rendering in a controlled context.
Creating a textarea or using DOMParser can decode entities in browsers. _.unescape() works consistently in Node.js and browsers without DOM APIs.
Did you know?
_.unescape is the inverse of _.escape()—but unescaping user content for innerHTML still requires sanitization. Prefer textContent when trust is unknown.