Open In App

Lodash _.snakeCase() Method

Last Updated : 20 Oct, 2023
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

Lodash _.snakeCase() method is used to convert a string to a snake case. Snake case refers to combining words into a lowercase string with underscores(_) between words. The string can be space-separated, dash-separated, or can be separated by underscores.

Syntax:

_.snakeCase( [string=''] );

Parameters:

  • string parameter holds the string to convert.

Return Value:

This method returns the snake-cased string.

Example 1: In this example, we are converting a string into a snake-case using the _snakeCase() method by passing the string into the method.

JavaScript
// Requiring the lodash library  
const _ = require("lodash");

// Use of _.snakeCase() method 
console.log(_.snakeCase('Geeks For Geeks'));
console.log(_.snakeCase('GeeksForGeeks'));
console.log(_.snakeCase('@#$%GeeksForGeeks@#$%'));

Output:

'geeks_for_geeks'
'geeks_for_geeks'
'geeks_for_geeks'

Example 2: In this example, we are converting a string into a snake-case using the _snakeCase() method by passing the string into the method. while null is not holding any string so it is not printing anything in the console.

JavaScript
// Requiring the lodash library  
const _ = require("lodash");  
    
// Use of _.snakeCase() method 
console.log(_.snakeCase(null)); 
console.log(_.snakeCase('123456789'));
console.log(_.snakeCase("gfg.Id"));

Output:

''
'123456789'
'gfg_id'

Similar Reads