<b>hi</b>
Say "hi" & bye
How It Works
Encode tags.< / > become < / >.
Encode quotes." becomes ".
Encode amp.& becomes &.
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 & Moe"
// back === raw → true
Coerce. Numbers and booleans are converted with '' + value.
Then escape. The string is scanned for special characters.
Handy. Safe when optional fields may be missing.
Compatibility
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 ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
_.escapeUnderscore 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 & Moe'. The ampersand is encoded so it will not start an HTML entity when inserted into a page.
Six: & → &, < → <, > → >, " → ", ' → ', and ` → `. 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.