Thursday, November 13, 2025
HomeLanguagesJavascriptLodash _.words() Method

Lodash _.words() Method

Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc.

The _.words() method is used to split the given string into an array of words. A pattern can be specified so that certain words can be removed from the string.

Syntax:

_.words( string, pattern )

Parameters: This method accepts two parameters as mentioned above and described below:

  • string: It is the string that is to be split. The default value is an empty string.
  • pattern: It is a pattern on the basis of which the words would be matched. It is an optional parameter.

Return Value: This method returns an array of words according to the pattern.

Example 1:

Javascript




// Defining Lodash variable 
const _ = require('lodash'); 
  
// Specify the string to split
var str = "Geeks for Geeks";
  
// Using _.words() method
console.log(_.words(str));


Output:

[ 'Geeks', 'for', 'Geeks' ]

Example 2:

Javascript




// Defining Lodash variable 
const _ = require('lodash'); 
  
// Specify the string to split
var str = "Geeks for Geeks";
  
// Using _.words() method
console.log(_.words(str, "for"));


Output:

[ 'for', index: 6, input: 'Geeks for Geeks', groups: undefined ]

Example 3:

Javascript




// Defining Lodash variable 
const _ = require('lodash'); 
  
// Specify the string to split
var str = "& Geeks for Geeks &";
  
// Using _.words() method with
// a given pattern
console.log(_.words(str, /[^, ]+/g));


Output:

[ '&', 'Geeks', 'for', 'Geeks', '&' ]
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32399 POSTS0 COMMENTS
Milvus
95 POSTS0 COMMENTS
Nango Kala
6765 POSTS0 COMMENTS
Nicole Veronica
11916 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11984 POSTS0 COMMENTS
Shaida Kate Naidoo
6889 POSTS0 COMMENTS
Ted Musemwa
7141 POSTS0 COMMENTS
Thapelo Manthata
6837 POSTS0 COMMENTS
Umr Jansen
6839 POSTS0 COMMENTS