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

01

Core Syntax

Call _.words(string) or pass a pattern.

02

Word array

Get tokens for analysis or UI tags.

03

Custom pattern

Pass RegExp for fine control.

04

vs split

Handles lodash word boundaries.

05

Word counts

Pair with _.size() or length.

06

Unicode

Works with many multilingual strings.

What Is _.words()?

_.words() splits a string into an array of words using lodash default word boundaries or an optional custom RegExp. Use it for word counts, keyword extraction, tag generation, and text analysis pipelines.

💡
Beginner tip

Think of _.words(article) as “tokenize this text for counting, tagging, or search” with smarter boundaries than split(' ').

📝 Syntax

javascript
_.words([string=''], [pattern])
javascript
import words from "lodash/words";

const tokens = words("Hello, Lodash words!");
// -> ["Hello", "Lodash", "words"]

⚡ Quick Reference

TaskCode patternResult
Default_.words('foo bar')['foo','bar']
Custom regex_.words(s, /[a-z]+/gi)Pattern control
Word count_.words(s).lengthToken count
Punctuation_.words('Hi!')Cleaner than split
Unicode_.words('你好,世界')CJK support
Importimport words from 'lodash/words'Per-method
Mutates?
No

Returns array

Pattern
Optional

Custom RegExp

vs split
Cleaner

Word rules

Use
Tokenize

Count & tags

🧰 Parameters

stringOptional

Source text to tokenize (default '').

patternOptional

RegExp or string pattern for word matching.

return valueArray

Array of extracted word strings.

default rulesLodash

Handles many Unicode letters and apostrophes in words.

Examples Gallery

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

📚 Getting Started

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

Example 1 — Extract words from plain text

Split Lorem ipsum into an array of word tokens.

javascript
const text = "Lorem ipsum dolor sit amet";
console.log(_.words(text));
// -> ["Lorem", "ipsum", "dolor", "sit", "amet"]
Try It Yourself

How It Works

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

Example 2 — Custom pattern without punctuation

Use /[a-zA-Z]+/g to strip trailing punctuation from tokens.

javascript
const phrase = "Hello, world!";
console.log(_.words(phrase, /[a-zA-Z]+/g));
// -> ["Hello", "world"]
Try It Yourself

How It Works

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

Example 3 — Unicode word extraction

Lodash word rules handle many CJK and other scripts.

javascript
const cn = "你好,世界";
console.log(_.words(cn));
Try It Yourself

How It Works

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

📈 Practical Patterns

Real-world formatting and data-handling scenarios.

Example 4 — Word count

Use _.words().length for a quick token count.

javascript
const bio = "JavaScript developer building web apps";
console.log(_.words(bio).length);

Example 5 — Generate keyword tags

Extract words from article content for tag chips.

javascript
const content = "Lodash words simplify text tokenization";
console.log(_.words(content));

Example 6 — Compare with split(' ')

split leaves punctuation attached to words; words() is cleaner for analysis.

javascript
const s = "Hello, world!";
console.log(s.split(" "));
console.log(_.words(s));

🧠 How _.words() Works

1

Receive string

Coerce input to string.

Input
2

Apply pattern

Use default word RegExp or custom pattern.

Match
3

Collect matches

Gather each word match into an array.

Extract
4

Return array

Array of word strings for downstream use.

Output

📝 Notes

  • _.words() returns an array, not a string.
  • Default rules attach apostrophes to words (e.g. don't is one token).
  • Pass a custom RegExp when default boundaries do not fit your locale.
  • split(' ') often leaves punctuation on tokens; _.words() is cleaner.
  • This is the last string-method tutorial—return to the String methods hub.
  • Official reference: lodash.com/docs/#words.

Conclusion

_.words() closes out the Lodash string tutorial series with flexible tokenization. Extract words for counts, tags, and analysis—or pass a custom pattern when defaults are not enough. Head back to the String methods hub to review the full catalog.

💡 Best Practices

✅ Do

  • Use default words() for quick token counts
  • Pass custom RegExp for locale-specific rules
  • Combine with filter/map for keyword pipelines
  • Test Unicode content for your target languages
  • Import lodash/words per method

❌ Don’t

  • Use split(' ') when punctuation breaks analysis
  • Assume default rules fit every language
  • Mutate the returned array expecting source change
  • Use as a full NLP tokenizer for complex grammar
  • Forget to handle empty strings (returns [])

Key Takeaways

📄02

Custom pattern

RegExp control

Advanced
📄03

vs split

Cleaner tokens

Compare
📄04

Analysis

Count & tags

Use case
📄05

String hub

Back to catalog

Nav

❓ Frequently Asked Questions

_.words() splits a string into an array of words using lodash default word rules or an optional custom pattern.
split(' ') breaks only on spaces and leaves punctuation attached. _.words() handles lodash word boundaries and supports custom RegExp patterns.
Yes. Pass a RegExp as the second argument to control what counts as a word—for example /[a-zA-Z]+/g to strip punctuation.
Lodash word rules handle many Unicode letter sequences. Test with your target locales for edge cases.
Word counts, keyword extraction, building tag lists, and tokenizing text before analysis or search indexing.
No. It returns a new array; the source string is unchanged.
Did you know?

_.words is the last dedicated string-method tutorial in this series—the String methods hub links the full catalog. Default rules handle many Unicode scripts; pass a custom RegExp when you need stricter control.

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