Lodash _.unescape() Method

Beginner
⏱️ 7 min read
📚 Updated: Jul 2026
🎯 6 Examples + 3 Try It
String utilities

What You’ll Learn

By the end of this tutorial, you’ll use Lodash’s _.unescape() confidently in real JavaScript projects.

01

Core Syntax

Call _.unescape(string) on escaped text.

02

Reverse escape

Pair with _.escape().

03

HTML entities

Decode &, <, and named entities.

04

Stored content

Render escaped API or DB strings.

05

Security

Sanitize before assigning to innerHTML.

06

Cross-environment

Works in Node.js and browsers.

What Is _.unescape()?

_.unescape() reverses HTML entity encoding—turning sequences like &amp; and &lt; 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.

📝 Syntax

javascript
_.unescape(string)
javascript
import unescape from "lodash/unescape";

const plain = unescape("Fast &amp; reliable &lt;widget&gt;");
// -> "Fast & reliable <widget>"

⚡ Quick Reference

TaskCode patternResult
Basic decode_.unescape('&lt;')<
Named entities_.unescape('&hearts;')
Round-trip_.unescape(_.escape(s))Restore literals
Quotes&quot; → "Attribute values
Pair with escape_.escape()Encode for storage
Importimport unescape from 'lodash/unescape'Per-method
Mutates?
No

Returns new string

Inverse
_.escape()

Encode/decode pair

Entities
HTML

Common set

Caution
XSS

Sanitize output

🧰 Parameters

stringRequired

The string containing HTML entities to decode.

return valueNew string

Decoded text with entities replaced by characters.

inverse_.escape()

Use escape when storing or transmitting HTML-sensitive text.

securityImportant

Unescaping user content for innerHTML requires sanitization.

Examples Gallery

Practical _.unescape() patterns with copy-ready code and interactive Try It Yourself labs.

📚 Getting Started

Core patterns for _.unescape() with copy-ready code.

Example 1 — Decode common HTML entities

Convert &, <, and > back to literal characters.

javascript
const escaped = "This &amp; That &lt; HTML &gt;";
const plain = _.unescape(escaped);

console.log(plain);
// -> "This & That < HTML >"
Try It Yourself

How It Works

Run the Try It editor to experiment with _.unescape() on your own strings.

Example 2 — Named entities (card suits)

Lodash decodes several named entities like &hearts;.

javascript
const suits = "Hearts: &hearts; Clubs: &clubs; Diamonds: &diams; Spades: &spades;";
console.log(_.unescape(suits));
Try It Yourself

How It Works

Run the Try It editor to experiment with _.unescape() on your own strings.

Example 3 — Round-trip with _.escape()

Escape for storage, unescape when rendering in a trusted pipeline.

javascript
const raw = 'Say "hello" & <welcome>';
const stored = _.escape(raw);
const restored = _.unescape(stored);

console.log("stored:", stored);
console.log("restored:", restored);
Try It Yourself

How It Works

Run the Try It editor to experiment with _.unescape() on your own strings.

📈 Practical Patterns

Real-world formatting and data-handling scenarios.

Example 4 — Quote entities in attributes

Unescape " when rebuilding attribute values from escaped sources.

javascript
const attr = "value=&quot;Lodash&quot;";
console.log(_.unescape(attr));

Example 5 — Process escaped API payload

Decode escaped description fields from a JSON API before display.

javascript
const item = { title: "Widget", body: "Fast &amp; reliable &lt;new&gt;" };
const displayBody = _.unescape(item.body);
console.log(displayBody);

Example 6 — Sanitize before innerHTML

Unescaping alone is not XSS protection—escape on output or use text nodes.

javascript
// Prefer textContent for user strings:
const user = _.unescape(userInput);
element.textContent = user;  // safe display

🧠 How _.unescape() Works

1

Receive string

Read the escaped source string.

Input
2

Match entities

Find &, <, named entities, and numeric forms.

Parse
3

Replace

Substitute each entity with its character.

Decode
4

Return plain text

New string with literals restored.

Output

📝 Notes

  • _.unescape() is non-mutating and returns a new string.
  • Pair with _.escape() for symmetric encode/decode.
  • Do not use unescaped user strings with innerHTML without sanitization.
  • Named entities like &hearts; decode to Unicode symbols.
  • For full HTML5 entity tables, consider a dedicated library.
  • Next: _.upperCase() for word-aware uppercasing.

Conclusion

_.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.

💡 Best Practices

✅ Do

  • 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

Key Takeaways

📄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 &amp; becomes & and &lt; becomes <.
Yes. _.escape() converts &, <, >, ", and ' to HTML entities. _.unescape() reverses those entities and a few named entities like &amp;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.

Practice _.unescape() in the Live Editor

Open the Try It editor and run the examples with your own strings.

Open Try It editor →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful