Lodash _.truncate() 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 _.truncate() confidently in real JavaScript projects.

01

Core Syntax

Call _.truncate(str, options) with length and omission.

02

Length limit

Set length to the max visible characters.

03

Omission suffix

Customize omission (default ...).

04

Word boundaries

Use separator to avoid mid-word cuts.

05

UI previews

Build card excerpts and table cell snippets.

06

Non-mutating

Original string stays intact.

What Is _.truncate()?

_.truncate() shortens a string to a maximum length and appends an omission (default ...) when the text exceeds that limit. Optional separator lets you cut at word boundaries instead of mid-word—ideal for card excerpts, notification previews, and table cells.

💡
Beginner tip

Think of _.truncate(text, { length: 80, separator: ' ' }) as “give me a readable preview that fits in this UI slot.” The original string is never modified.

📝 Syntax

javascript
_.truncate(string, [options])
javascript
import truncate from "lodash/truncate";

const excerpt = truncate(
  "Lodash truncate builds readable UI previews from long text.",
  { length: 35, separator: " " }
);
// -> "Lodash truncate builds readable..."

⚡ Quick Reference

TaskCode patternResult
Default truncate_.truncate(str, { length: 30 })Adds ... when over limit
Custom omission{ length: 50, omission: '…' }Unicode ellipsis
Word boundary{ length: 40, separator: ' ' }No mid-word cut
Regex separator{ separator: /,? +/ }Split at commas/spaces
Under limit_.truncate('Hi', { length: 10 })Returns unchanged
Importimport truncate from 'lodash/truncate'Tree-shakeable
Mutates?
No

Returns new string

Default omission
...

Configurable

Separator
space

Word-aware cut

Best for
UI excerpts

Cards & previews

🧰 Parameters

stringRequired

The source string to truncate. Non-strings are coerced.

options.lengthOptional

Maximum length of the result including omission (default 30).

options.omissionOptional

Suffix when truncated (default '...').

options.separatorOptional

Truncate at last separator before limit (default ' ').

Examples Gallery

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

📚 Getting Started

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

Example 1 — Basic truncation with default omission

Truncate Lorem ipsum to 20 characters with the default ... suffix.

javascript
const longText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
const truncated = _.truncate(longText, { length: 20 });

console.log(truncated);
// -> "Lorem ipsum dolor..."
Try It Yourself

How It Works

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

Example 2 — Custom omission string

Use a [Read more] suffix for article previews instead of ellipsis.

javascript
const article = "Lodash truncate helps you build readable excerpts in cards and lists.";
const preview = _.truncate(article, { length: 40, omission: " [Read more]" });

console.log(preview);
Try It Yourself

How It Works

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

Example 3 — Word-aware truncation with separator

Truncate at the last space before the limit so words are not cut in half.

javascript
const title = "Understanding Lodash string utilities in depth";
const cardTitle = _.truncate(title, { length: 30, separator: " " });

console.log(cardTitle);
// -> "Understanding Lodash string..."
Try It Yourself

How It Works

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

📈 Practical Patterns

Real-world formatting and data-handling scenarios.

Example 4 — String already under the limit

When text is shorter than length, no omission is appended.

javascript
const short = "Hello";
console.log(_.truncate(short, { length: 20 }));
// -> "Hello"

Example 5 — Regex separator for punctuation

Truncate at commas or spaces using a regex separator.

javascript
const csv = "apple, banana, cherry, date, elderberry";
const snippet = _.truncate(csv, { length: 18, separator: /,? +/ });

console.log(snippet);

Example 6 — Compare with native slice

Native slice cuts exactly at an index without omission or word awareness.

javascript
const text = "Hello world from Lodash";
const lodashWay = _.truncate(text, { length: 12, separator: " " });
const nativeWay = text.slice(0, 12) + "...";

console.log("lodash:", lodashWay);
console.log("native:", nativeWay);

🧠 How _.truncate() Works

1

Receive string

Lodash coerces the input to a string.

Input
2

Check length

If length is within the limit, return unchanged.

Compare
3

Find cut point

Apply separator to avoid mid-word breaks when configured.

Separator
4

Append omission

Slice and append the omission suffix (default ...).

Result

📝 Notes

  • _.truncate() is non-mutating—strings are immutable in JavaScript.
  • The length option includes the omission string in the total character count.
  • Use separator: ' ' for word-aware previews in UI cards.
  • Strings already shorter than length are returned without an omission.
  • For exact byte limits (URLs, APIs), validate encoding separately—truncate counts characters.
  • Previous: _.trimStart() for leading whitespace removal.

Conclusion

Use _.truncate() whenever UI space is limited and readability matters. Configure length, omission, and separator once per component and reuse across card lists, notifications, and data tables.

💡 Best Practices

✅ Do

  • Set length to fit your UI component, including omission width
  • Use separator: ' ' for readable word-boundary cuts
  • Reuse one truncate helper across card and list components
  • Test with your longest real content samples
  • Import lodash/truncate for bundle size

❌ Don’t

  • Truncate security-sensitive text without reviewing full content links
  • Assume length counts bytes instead of characters
  • Cut mid-word in headings when separator can help
  • Use truncate for validation—use max-length on input instead
  • Forget that omission counts toward length

Key Takeaways

📄02

Length + omission

Fit UI slots

Core
📄03

Separator

Word-aware cuts

Pattern
📄04

UI previews

Cards & tables

Use case
📄05

trimStart

Prior method

Nav

❓ Frequently Asked Questions

_.truncate() shortens a string to a maximum length and appends an omission string (default '...') when the text is longer than the limit.
No. JavaScript strings are immutable. _.truncate() always returns a new string.
An options object with length (max characters), omission (suffix when truncated, default '...'), and separator (truncate at last occurrence of separator before the limit, default a space).
slice() cuts at an exact index and does not add an omission or respect word boundaries. _.truncate() is built for readable UI excerpts with ellipsis.
When set, truncation happens at the last separator before the length limit so you do not cut mid-word. Use separator: ' ' for word-aware previews or a regex for finer control.
Use it for card excerpts, table cells, notification previews, and any UI that must show a short, readable snippet of longer text.
Did you know?

_.truncate counts the omission toward length—so a 30-character limit with ... leaves 27 characters of visible text. Use separator to avoid awkward mid-word cuts in headings.

Practice _.truncate() 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