Converts the characters &, <, >, ", and ' in a string to their corresponding HTML entities. Useful when you build HTML by string concatenation. Part of Lodash String (stable — not deprecated).
Pass the string and options shown below. Check the return type before you chain another helper.
Syntax
Call _.escape. Start with this line.
Lodash
_.escape([string=''])
Call
What it does
_.escape([string=''])
Primary signature.
_.escape(string)
Transform or inspect a string.
Prefer import escape from "lodash/escape" when you want a smaller bundle.
Library declarations
Lodash
_.escape([string=''])
Parameters
Parameter
Description
string
Optional. Input to encode
Return Value
Return Value
Description
*
New string with entities
Lodash Examples
Example 1 — Official: Escape &
Encode an ampersand in a name list.
Lodash
_.escape('fred, barney, & pebbles');
// → 'fred, barney, & pebbles'
_.escape is a Lodash 4.x String helper. It runs anywhere Lodash runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install lodash or lodash/escape. Native alternative: assign with textContent, or use your framework’s auto-escaping.
✓ Stable · Lodash 4.x
_.escape()
Available in [email protected] and lodash/escape modular import. Works in evergreen browsers and current Node LTS when Lodash is installed.
100%With Lodash 4.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
_.escapeLodash 4.x
Bottom line: Safe for production. Escape on HTML output; store raw text when you can.
Frequently Asked Questions
_.escape([string='']) converts &, <, >, ", and ' to their HTML entities so the string shows as text, not markup. Official: _.escape('fred, barney, & pebbles') → 'fred, barney, & pebbles'.
No. It is one layer for HTML text context. Also use CSP, framework escaping, validation, and safe APIs like textContent.