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

01

Core syntax

_.snakeCase(string)

02

Word splitting

Handles camelCase & spaces

03

API keys

Normalize JSON field names

04

DB columns

Match SQL conventions

05

vs kebabCase

Underscores vs hyphens

06

Production tips

Consistent naming rules

What Is _.snakeCase()?

_.snakeCase() converts any string into snake_case—lowercase words joined by underscores. It is the go-to helper when JavaScript identifiers (camelCase) must become database column names, Python-style constants, or REST path segments.

💡
Beginner tip

Think of _.snakeCase('getUserProfile') as “give me get_user_profile for the API schema.” The original string is untouched.

📝 Syntax

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

Syntax Rules

  • string — The value to convert (defaults to empty string).
  • Return value — A new snake_case string.
  • Word boundaries — Splits on camelCase transitions, spaces, hyphens, and punctuation.
  • Lowercase — All letters in the result are lowercase.
  • Separator — Words are joined with a single underscore _.
javascript
import snakeCase from "lodash/snakeCase";

const input = "helloWorldExample";
const result = snakeCase(input);
// -> "hello_world_example"

⚡ Quick Reference

TaskCode patternResult
camelCase input_.snakeCase('fooBar')foo_bar
Spaces_.snakeCase('Foo Bar')foo_bar
API path_.snakeCase('getUser')get_user
DB column_.snakeCase('firstName')first_name
vs kebabCase_.kebabCase('fooBar')foo-bar
Custom sep_.snakeCase(s).replace(/_/g, '-')hyphen variant
Mutates?
No

Returns new string

Case
lower

All lowercase output

Separator
_

Underscore join

Related
kebabCase

Hyphen variant

🧰 Parameters

string Required

The string to convert. Handles camelCase, spaces, and special characters.

_.snakeCase('helloWorld')
return value New string

snake_case result; original unchanged.

-> 'hello_world'
word rules Built-in

Lodash splits compound identifiers before joining.

// fooBar -> foo_bar
empty input Safe

Empty or missing string yields empty result.

_.snakeCase('')

Examples Gallery

Practical _.snakeCase() patterns with sample output and interactive Try It Yourself labs.

📚 Getting Started

Convert common identifier formats to snake_case.

Example 1 — Basic camelCase conversion

Turn a camelCase variable name into snake_case for an API payload key.

javascript
const camelCaseString = "helloWorldExample";
const snakeCaseString = _.snakeCase(camelCaseString);

console.log(snakeCaseString);
// -> "hello_world_example"
Try It Yourself

How It Works

Lodash detects word boundaries in camelCase and joins lowercase segments with underscores.

📚 Practical Patterns

Handle spaces, punctuation, and real-world naming.

Example 2 — Special characters and spaces

Normalize a human-readable phrase with punctuation into snake_case.

javascript
const messy = "Hello, World! How are you?";
const clean = _.snakeCase(messy);

console.log(clean);
// -> "hello_world_how_are_you"
Try It Yourself

How It Works

Punctuation and whitespace become word breaks; letters are lowercased.

Example 3 — API endpoint formatting

Format a method name into a REST-style path segment.

javascript
const endpointName = "getUserDetails";
const formattedEndpoint = _.snakeCase(endpointName);

console.log("/api/" + formattedEndpoint);
// -> /api/get_user_details
Try It Yourself

How It Works

Pair snake_case segments with your router convention for predictable URLs.

Example 4 — Database field names

Map a JavaScript property to a SQL-friendly column name.

javascript
const fieldName = "firstName";
const column = _.snakeCase(fieldName);
// -> "first_name"

Example 5 — File naming

Generate a safe filename from a display title.

javascript
const fileName = "MyDocumentTitle";
const safe = _.snakeCase(fileName) + ".pdf";
// -> "my_document_title.pdf"

📚 Beyond the Basics

Compare with related case helpers.

Example 6 — vs kebabCase and camelCase

Pick the casing style your system expects.

javascript
const id = "userProfileId";

console.log(_.snakeCase(id));  // user_profile_id
console.log(_.kebabCase(id));  // user-profile-id
console.log(_.camelCase(id));  // userProfileId

How It Works

Use snake_case for SQL/JSON keys, kebab-case for URLs/CSS, camelCase for JS properties.

📋 Related operations

Topic_.snakeCase_.kebabCase_.camelCase
SeparatorUnderscore _Hyphen -None (camel)
Typical useDB, Python APIsURLs, CSSJS properties
MutatesNoNoNo
InputAny stringAny stringAny string

🧠 How _.snakeCase() Works

1

Parse words

Lodash splits the input into words using case and delimiter rules.

Parse
2

Lowercase

Each word is converted to lowercase.

Case
3

Join

Words are concatenated with underscore separators.

Join
=

Return

A new snake_case string is returned.

📝 Notes

  • _.snakeCase() is non-mutating—assign the result to a new variable.
  • Output is always lowercase with underscore separators.
  • For hyphen-separated output, use _.kebabCase().
  • Acronyms may be split unexpectedly—verify output for domain-specific tokens.
  • Import lodash/snakeCase for tree-shaking in modern bundles.

Conclusion

_.snakeCase() is a practical Lodash string helper. Use the patterns above in your projects and explore the next method in the series.

❓ Frequently Asked Questions

It converts a string to snake_case: words are lowercased and separated by underscores. Input like helloWorld or Hello World becomes hello_world.
No. JavaScript strings are immutable. _.snakeCase() always returns a new string.
snake_case uses underscores between words; kebab-case uses hyphens. Lodash provides both _.snakeCase() and _.kebabCase() with the same word-splitting rules.
_.snakeCase() always uses underscores. For a custom separator, convert with _.snakeCase() then replace underscores, or use _.words() with your own join logic.
camelCase, PascalCase, spaces, hyphens, and mixed punctuation are normalized into word boundaries before casing.
For one-off transforms in modern codebases, manual regex works. Lodash is handy when you want consistent word splitting across many identifiers.
Did you know?

_.snakeCase() lowercases words and joins them with underscores—ideal for JSON keys, SQL column names, and REST path segments.

Practice _.snakeCase() in the Live Editor

Open the Try It editor and run the examples from this tutorial.

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