Underscore.js _.escape() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Utility

Definition

Escapes a string for insertion into HTML, replacing &, <, >, ", `, and ' (official Underscore docs).

Pass the required arguments shown below. Underscore utility helpers usually return a new value or function — check edge cases before you ship.

Syntax

Call _.escape. Start with this line.

Underscore
_.escape(string)
CallWhat it does
_.escape(string)Primary signature.
_.escape(...)Call with the arguments your case needs.

Load Underscore with import _ from "underscore" (or the UMD CDN build) so these helpers are available as _.

Library declarations
Underscore
_.escape(string)

Parameters

ParameterDescription
stringRequired. Text to encode (*null/undefined → "")

Return Value

Return ValueDescription
*New string with HTML entities

Underscore.js Examples

Example 1 — Official Ampersand

Encode & so the Stooges’ names are safe in HTML.

Underscore
_.escape('Curly, Larry & Moe');
// → "Curly, Larry &amp; Moe"
Try It Yourself

How It Works

  1. Start with text. The string contains a bare &.
  2. Find specials. Underscore matches characters in its escape map.
  3. Replace &. It becomes &amp;.
  4. Safe insert. The browser shows an ampersand, not an entity start.

Example 2 — Escape Angle Brackets

Turn markup-looking text into displayable entities.

Underscore
_.escape('<b>hi</b>');
// → "&lt;b&gt;hi&lt;/b&gt;"

_.escape('Say "hi" & bye');
// → "Say &quot;hi&quot; &amp; bye"
Try It Yourself

How It Works

  1. Encode tags. < / > become &lt; / &gt;.
  2. Encode quotes. " becomes &quot;.
  3. Encode amp. & becomes &amp;.
  4. Result. Safe to place inside HTML text or attributes.

Example 3 — Escape Then Unescape

_.unescape reverses the encoding from _.escape.

Underscore
var raw = 'Curly, Larry & Moe';
var encoded = _.escape(raw);
var back = _.unescape(encoded);

// encoded → "Curly, Larry &amp; Moe"
// back === raw → true
Try It Yourself

How It Works

  1. Escape first. Special characters become entities.
  2. Unescape next. Entities map back to the originals.
  3. Round-trip. You get the same string for these six characters.
  4. Rule. Escape on output; unescape only when you need the raw text again.

Example 4 — Null, Undefined & Coercion

Nullish values become an empty string; other values are stringified first.

Underscore
_.escape(null);       // → ""
_.escape(undefined);  // → ""
_.escape(42);         // → "42"
_.escape(true);       // → "true"
Try It Yourself

How It Works

  1. Nullish. null / undefined become "" — no throw.
  2. Coerce. Numbers and booleans are converted with '' + value.
  3. Then escape. The string is scanned for special characters.
  4. Handy. Safe when optional fields may be missing.

Browser & Runtime Support

_.escape ships with Underscore.js (1.x). It runs anywhere Underscore runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install underscore.

✓ Stable · Underscore 1.x

_.escape()

Available in [email protected] (UMD / ESM). Works in evergreen browsers and current Node LTS when Underscore is loaded.

100% With Underscore 1.x
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
_.escape Underscore 1.x+

Bottom line: Safe for production once Underscore is on the page or in your bundle. Not a native browser API — load Underscore first. Prefer a pinned CDN version (e.g. [email protected]).

Frequently Asked Questions

_.escape(string) encodes a string for insertion into HTML. It replaces &, <, >, ", `, and ' with HTML entities so the text displays instead of becoming markup.
_.escape('Curly, Larry & Moe') returns 'Curly, Larry &amp; Moe'. The ampersand is encoded so it will not start an HTML entity when inserted into a page.
Six: & → &amp;, < → &lt;, > → &gt;, " → &quot;, ' → &#x27;, and ` → &#x60;. Slash / is not escaped (since Underscore 1.5.2).
Use _.unescape(string). It is the opposite helper: _.unescape(_.escape('Curly, Larry & Moe')) returns the original names.
No. It is one layer for HTML text and attributes. Prefer textContent when you can. It does not sanitize javascript: URLs, event handlers, or CSS. Escape once on output.
No. It is a stable Underscore utility. Version 1.5.2 stopped escaping /. Version 1.7.0 also encodes backticks. Null/undefined become an empty string.

Next: _.unescape()

Learn how to reverse HTML entity encoding and get the original characters back.

_.unescape() tutorial →

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.

10 people found this page helpful