Saturday, August 30, 2025
HomeLanguagesJavascriptLodash _.trim() Method

Lodash _.trim() Method

The Lodash _.trim() Method removes leading and trailing white spaces or specified characters from the given string.

Syntax:

_.trim( string, chars )

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

  • string: This parameter holds the string that need to be trimmed.
  • chars: This parameter holds the charset to be specifically trimmed.

Return Value: This method returns a trimmed string

Example 1:

Javascript




// Defining Lodash variable 
const _ = require('lodash'); 
  
var str = "   Geeks-for-Geeks   ";
  
// Using _.trim() method
console.log(_.trim(str));


Output:

Geeks-for-Geeks

Example 2: explicitly trimming “-” from the string.

Javascript




// Defining Lodash variable 
const _ = require('lodash'); 
  
var str = "----neveropen----";
  
// Using _.trim() method
console.log(_.trim(str, "-"))


Output:

neveropen

Note: This will not work in normal JavaScript because it requires the lodash library to be installed and can be installed using npm install lodash.

RELATED ARTICLES

Most Popular

Dominic
32249 POSTS0 COMMENTS
Milvus
80 POSTS0 COMMENTS
Nango Kala
6617 POSTS0 COMMENTS
Nicole Veronica
11792 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11838 POSTS0 COMMENTS
Shaida Kate Naidoo
6731 POSTS0 COMMENTS
Ted Musemwa
7012 POSTS0 COMMENTS
Thapelo Manthata
6689 POSTS0 COMMENTS
Umr Jansen
6701 POSTS0 COMMENTS