Lodash _.snakeCase() Method

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('Beginner For Beginner'));
console.log(_.snakeCase('w3wiki'));
console.log(_.snakeCase('@#$%w3wiki@#$%'));


Output:

'Beginner_for_Beginner'
'Beginner_for_Beginner'
'Beginner_for_Beginner'

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'