Skip to main content

snakeCase()

snakeCase(str): string

Converts a string to snake_case.

note

Handles camelCase, PascalCase, acronyms, kebab-case, and spaces.


Parametersโ€‹

str: stringโ€‹

The string to convert.


Returns: stringโ€‹

The string in snake_case.


Sinceโ€‹

2.0.0


Performanceโ€‹

O(n) where n is string length. Single regex pass with match + join.


Also known asโ€‹

snake (Radashi) ยท snakeCase (Lodash, es-toolkit, Modern Dash) ยท toSnakeCase (Remeda) ยท โŒ (Ramda, Effect, Antfu)


Exampleโ€‹

snakeCase('helloWorld');     // => 'hello_world'
snakeCase('XMLHttpRequest'); // => 'xml_http_request'
snakeCase('hello-world'); // => 'hello_world'
snakeCase('Hello World'); // => 'hello_world'
snakeCase('--foo--bar--'); // => 'foo_bar'

How it works?โ€‹

Converts a string to snake_case format. All lowercase, words separated by underscores.

Conversion Examplesโ€‹

InputOutput
helloWorldhello_world
XMLHttpRequestxml_http_request
hello-worldhello_world
Hello Worldhello_world

Processโ€‹


Use Casesโ€‹

Format database columns ๐Ÿ“Œโ€‹

Convert strings to snake_case for database column names or Python API compatibility. Critical for backend integration.

const column = snakeCase('userId'); // 'user_id'

Convert payload keys before sending to a Python/Ruby APIโ€‹

Transform camelCase JavaScript keys into snake_case before sending to a backend API. The reverse of the camelCase normalization โ€” essential for full-stack interop.

const payload = {
userId: 42,
firstName: "Alice",
isActive: true,
};

// Combined with mapKeys for full object transformation
const apiPayload = mapKeys(payload, (_, key) => snakeCase(key));
// => { user_id: 42, first_name: "Alice", is_active: true }

await fetch("/api/users", {
method: "POST",
body: JSON.stringify(apiPayload),
});