Lodash _.words() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
String

Definition

Splits string into an array of its words. With no pattern, Lodash uses Unicode-aware word rules (including camelCase breaks). Part of Lodash String since 3.0.0 (stable — not deprecated).

Pass the string and options shown below. Check the return type before you chain another helper.

Syntax

Call _.words. Start with this line.

Lodash
_.words([string=''], [pattern])
CallWhat it does
_.words([string=''], [pattern])Primary signature.
_.words(string)Transform or inspect a string.

Prefer import words from "lodash/words" when you want a smaller bundle.

Library declarations
Lodash
_.words([string=''], [pattern])

Parameters

ParameterDescription
stringOptional. The string to inspect (default '')
patternOptional. RegExp or string used to match words

Return Value

Return ValueDescription
*Array of matched words

Lodash Examples

Example 1 — Official: Default Word Rules

Punctuation and & are skipped; letter words remain.

Lodash
_.words('fred, barney, & pebbles');
// → ['fred', 'barney', 'pebbles']
Try It Yourself

How It Works

  1. Input. A list of names with commas and &.
  2. Default pattern. Matches Unicode word sequences.
  3. Skip. Commas, spaces, and & are not words.
  4. Result. Three name tokens.

Example 2 — Official: Custom Pattern

A RegExp can keep tokens that the default rules drop.

Lodash
_.words('fred, barney, & pebbles', /[^, ]+/g);
// → ['fred', 'barney', '&', 'pebbles']
Try It Yourself

How It Works

  1. Pattern. /[^, ]+/g matches runs that are not comma or space.
  2. Match. & qualifies as a token.
  3. Global flag. The g flag finds every match.
  4. Use. Customize tokenization for CSV-like text.

Example 3 — camelCase and Numbers

Default rules break on case changes and digit runs.

Lodash
_.words('fooBar');
// → ['foo', 'Bar']

_.words('XMLHttpRequest');
// → ['XML', 'Http', 'Request']

_.words('version2Update');
// → ['version', '2', 'Update']
Try It Yourself

How It Works

  1. camelCase. Lower→Upper boundary starts a new word.
  2. Acronyms. XML stays together before Http.
  3. Digits. Number runs become their own tokens.
  4. Why it matters. Case helpers like _.kebabCase rely on this.

Example 4 — Count Words

Use the array length for a simple word count.

Lodash
var text = 'Learn Lodash words today';
var parts = _.words(text);

parts.length;
// → 4

parts.join('-');
// → 'Learn-Lodash-words-today'
Try It Yourself

How It Works

  1. Tokenize. _.words returns four words.
  2. Count. .length is the word count.
  3. Join. Rebuild a slug-like string with join.
  4. Next step. Prefer _.kebabCase for full case conversion.

Example 5 — Empty Input and Digits-Only

Empty strings return []; digit runs still count as words.

Lodash
_.words('');
// → []

_.words('   ');
// → []

_.words('404 error 12');
// → ['404', 'error', '12']
Try It Yourself

How It Works

  1. Empty. No matches → [], not null.
  2. Whitespace. Spaces alone produce no words.
  3. Numbers. Digit sequences are valid default words.
  4. Safe. Easy to chain with .length or .map.

Browser & Runtime Support

_.words is a Lodash String helper (available since 3.0.0, stable in 4.x). It runs anywhere Lodash runs — modern browsers (via CDN or bundle) and Node.js / Bun / Deno with npm install lodash.

✓ Stable · Lodash 3.0.0+

_.words()

Available in [email protected] full builds and the lodash/words modular import. Works in evergreen browsers and current Node LTS when Lodash is installed.

100% With Lodash 4.x
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
_.words Lodash 3.0.0+

Bottom line: Safe for production once Lodash is on the page or in your bundle. Prefer import words from "lodash/words" for smaller builds. Pass a custom pattern when default Unicode/camelCase rules are not what you want.

Frequently Asked Questions

_.words([string=''], [pattern]) splits string into an array of its words. With no pattern, Lodash uses its default Unicode word rules.
split(' ') breaks only on spaces and keeps punctuation attached. _.words() uses word boundaries and accepts a custom RegExp or string pattern.
Yes. Pass a RegExp or string as the second argument to control what counts as a word — for example /[^, ]+/g.
Yes. The default pattern treats camelCase and PascalCase transitions as word boundaries, which powers helpers like _.camelCase and _.kebabCase.
_.words('') returns []. Missing string also defaults to ''.
Use import words from "lodash/words" or require("lodash/words"). Full build: import _ from "lodash" then _.words.

Next: Lodash String methods

Browse the full list of Lodash string helpers and tutorials.

String methods hub →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

10 people found this page helpful