Lodash String methods

Beginner
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 2 Code examples
Lodash

What you’ll learn

  • What to know before you start (see Prerequisites).
  • How the String category maps to everyday tasks (case styles, cleanup, escaping, display).
  • When Lodash string helpers add value over native String methods.
  • How to import individual string functions for smaller bundles.
  • Where to open each _.methodName tutorial on CodeToFun as those pages land.

Prerequisites

Basic JavaScript strings, template literals, and optional familiarity with Seq chaining when you format strings inside pipelines.

  • String immutability: methods return new strings; the original value never changes.
  • Native baseline: know trim, toLowerCase, startsWith, and padStart so you can choose lodash deliberately.
  • Unicode awareness: case and word splitting can surprise with accented characters—see deburr when normalizing user input.

Key concepts

Lodash string helpers cluster around four jobs: normalize text, convert naming styles, prepare safe output, and shape strings for UI display.

Case styles

camelCase, kebabCase, snakeCase, startCase for APIs, CSS, and databases.

Escape & template

escape, unescape, and template for HTML snippets and compiled strings.

Trim & pad

trim, padStart, padEnd for cleanup and fixed-width display.

Display limits

truncate, words, and capitalize for previews and labels.

Overview

Lodash String is a focused toolkit for text you handle in apps—user names, slugs, log lines, form values, and HTML fragments—with predictable rules across browsers and Node.js.

Identifiers

Turn labels into camelCase props or kebab-case CSS classes.

Safety

escape user text before inserting into HTML templates.

UI copy

truncate long descriptions with an ellipsis in cards and tables.

⚖️ Lodash vs native strings

Modern JavaScript strings are powerful. Reach for Lodash when you need word-aware casing, lodash-specific truncation, HTML escape helpers, or template compilation in one consistent API.

SituationPrefer nativeConsider Lodash
Simple trim / padstr.trim(), padStarttrim with custom char sets; lodash parity in older targets
camelCase / kebab-case slugsManual regex (error-prone)camelCase, kebabCase, snakeCase
HTML entity escapeDOM APIs or librariesescape / unescape for quick server output
Ellipsis previewsSlice + append "..."truncate with word-aware options
Compiled templatesTemplate literalstemplate when you need lodash delimiter syntax
1

Install and import

Import only the string helpers you call so bundlers can tree-shake the rest of Lodash.

Terminal
npm install lodash
javascript
import camelCase from "lodash/camelCase";
import truncate from "lodash/truncate";

const slug = camelCase("hello world");
// -> "helloWorld"

const preview = truncate("A long product description for the card UI", {
  length: 30
});
// -> "A long product descriptio..."
2

Case conversion pipeline

Chain lodash string steps (or call them in sequence) when normalizing user-facing labels into code identifiers.

javascript
import deburr from "lodash/deburr";
import kebabCase from "lodash/kebabCase";
import startCase from "lodash/startCase";

const raw = "  Café & Crêpes  ";

const display = startCase(deburr(raw.trim()));
// -> "Cafe And Crepes"

const cssClass = kebabCase(display);
// -> "cafe-and-crepes"

🔄 Strings are always immutable

Every Lodash string helper returns a new string (or boolean, number, or array). The input is never modified—safe to use in React state and reducers without cloning first.

InputTypical returnOriginal changed?
" hello "trim"hello"No
"foo bar"camelCase"fooBar"No
"<b>"escape"&lt;b&gt;"No
"a,b,c"split["a","b","c"]No

Suggested learning path

Walk these in order when exploring lodash strings for the first time.

  1. Cleanup: trim, padStart, deburr.
  2. Case styles: camelCase, kebabCase, snakeCase.
  3. Display: truncate, capitalize, words.
  4. Safety: escape and template (use templates carefully).
  5. Checks: startsWith / endsWith for URL and file paths.

💻 Environment and versions

  • Lodash 4.x: the method index below matches the String exports shipped with lodash@^4 on npm.
  • Browsers & Node: string helpers run identically; no DOM is required except when you insert escaped HTML yourself.
  • TypeScript: install @types/lodash for per-method import typings.

Method index

Each row links to a focused tutorial when published. URLs follow /lodash/string/{method-lowercase} (for example /lodash/string/camel-case).

MethodWhat it does
_.camelCase()Convert a string to camelCase (e.g. "foo bar" → "fooBar").
_.capitalize()Uppercase the first character; leave the rest unchanged.
_.deburr()Remove combining diacritical marks (deburred Latin letters).
_.endsWith()Return true if string ends with the given target (optional position).
_.escape()Escape HTML special characters (&, <, >, ", ') for safe text output.
_.escapeRegExp()Escape RegExp metacharacters so a string can be used literally in a pattern.
_.kebabCase()Convert a string to kebab-case (e.g. "fooBar" → "foo-bar").
_.lowerCase()Convert the entire string to lower case (lodash case rules).
_.lowerFirst()Lowercase only the first character of the string.
_.pad()Pad string on left and right to a target length with fill characters.
_.padEnd()Pad the end of a string to a target length.
_.padStart()Pad the start of a string to a target length.
_.parseInt()Parse an integer from a string with consistent radix handling.
_.repeat()Repeat a string n times (non-negative integer).
_.replace()Replace matches in a string (lodash wrapper around replacement patterns).
_.snakeCase()Convert a string to snake_case (e.g. "fooBar" → "foo_bar").
_.split()Split a string into an array by separator (lodash wrapper).
_.startCase()Convert to start case with capitalized words (e.g. "foo-bar" → "Foo Bar").
_.startsWith()Return true if string starts with the given target (optional position).
_.template()Compile a template string with lodash delimiter syntax (interpolate, escape, and evaluate tags).
_.toLower()Convert string to lower case (alias-style helper).
_.toUpper()Convert string to upper case (alias-style helper).
_.trim()Remove leading and trailing whitespace (or custom chars).
_.trimEnd()Remove trailing whitespace (or custom chars).
_.trimStart()Remove leading whitespace (or custom chars).
_.truncate()Truncate a string with an omission when longer than a length limit.
_.unescape()Reverse HTML entity escaping from _.escape().
_.upperCase()Convert the entire string to upper case (lodash case rules).
_.upperFirst()Uppercase only the first character of the string.
_.words()Split a string into an array of words using lodash word rules.

Pitfalls to avoid

Template

Untrusted template source

_.template() compiles strings as functions—never pass user-authored template text without strict sandboxing.

Case

Expecting locale-aware casing

Lodash case helpers use lodash rules, not toLocaleLowerCase—verify behavior for your language if i18n matters.

Bundle

Importing all of lodash for one trim

Use lodash/trim or native trim when that is all you need.

❓ FAQ

It groups helpers for formatting, cleaning, casing, escaping, padding, and templating strings with consistent cross-environment behavior.
Native String methods cover many basics (trim, toLowerCase, startsWith). Lodash shines for case conversion (camelCase, kebabCase), deburr, truncate with omission, escape/unescape, and template compilation.
No. JavaScript strings are immutable. Every Lodash string helper returns a new string (or boolean/number/array) without changing the input.
Use per-method packages like lodash/camelcase or tree-shakeable ESM imports instead of importing the entire lodash bundle when you only need one helper.
Treat templates like code: never compile untrusted user strings as template source. Use fixed templates and pass sanitized data, or prefer a dedicated templating library with sandboxing for user-authored content.
Each method page lives at /lodash/string/{method-lowercase}, for example /lodash/string/camel-case for _.camelCase().

Summary

  • Scope: Lodash String covers casing, trimming, padding, escaping, templating, truncation, and word utilities.
  • Immutable: all helpers return new values; inputs stay unchanged.
  • Next step: open Lodash _.camelCase(), revisit _.thru(), or pick any row in the index.
Did you know?

Lodash toLower and toUpper mirror common naming from other languages, while lowerCase and upperCase apply lodash’s word-aware casing rules—they are not always identical for multi-word strings.

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.

9 people found this page helpful