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
Fundamentals
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.
Foundation
📝 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"
Cheat Sheet
⚡ Quick Reference
Task
Code pattern
Result
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
Reference
🧰 Parameters
stringRequired
The string to convert. Handles camelCase, spaces, and special characters.
_.snakeCase('helloWorld')
return valueNew string
snake_case result; original unchanged.
-> 'hello_world'
word rulesBuilt-in
Lodash splits compound identifiers before joining.
// fooBar -> foo_bar
empty inputSafe
Empty or missing string yields empty result.
_.snakeCase('')
Hands-On
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.
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.