Default Omission String in Lodash Truncate

This article explains the default omission string used by the _.truncate method in the Lodash JavaScript library, how it affects truncated output length, and how developers can configure or override this setting for custom string handling in their applications.

In the Lodash library, the default omission string appended by the _.truncate function is '...' (three standard ASCII periods). When a string exceeds the specified maximum length, Lodash shortens the text and appends these three dots to indicate that content has been omitted.

How the Omission String Works

By default, _.truncate limits text to a total length of 30 characters, which includes the length of the omission string itself. Because the default omission string '...' is three characters long, Lodash retains the first 27 characters of the original text and appends '...' to meet the 30-character limit.

const _ = require('lodash');

const text = 'This is a long string that will be truncated by Lodash.';
const result = _.truncate(text);

console.log(result);
// Output: "This is a long string that w..."
console.log(result.length);
// Output: 30

Customizing the Omission String

You can override the default omission string by passing an options object with an omission property to _.truncate. This is useful if you prefer a single Unicode ellipsis character ('…'), a custom suffix like ' [more]', or an empty string ('').

const _ = require('lodash');

const text = 'This is a long string that will be truncated by Lodash.';

// Using a Unicode ellipsis
const customEllipsis = _.truncate(text, {
  length: 24,
  omission: '…'
});
console.log(customEllipsis);
// Output: "This is a long string t…"

// Using a text label
const customLabel = _.truncate(text, {
  length: 28,
  omission: ' [read more]'
});
console.log(customLabel);
// Output: "This is a long s [read more]"

Lodash always accounts for the length of your custom omission string against the specified length option. If the provided length is shorter than the omission string itself, the returned string will simply be the omission string sliced to fit the maximum length.