docs/reference/compat/string/snakeCase.md
::: warning Use snakeCase from es-toolkit
This snakeCase function operates slowly due to normalization logic for handling null or undefined.
Instead, use the faster and more modern snakeCase from es-toolkit.
:::
Converts a string to snake case.
const snakeCased = snakeCase(str);
snakeCase(str)Use snakeCase when you want to convert a string to snakecase. Snake case is a naming convention where each word is written in lowercase and connected with underscores ().
import { snakeCase } from 'es-toolkit/compat';
// Convert camel case
snakeCase('camelCase');
// Returns: 'camel_case'
// Convert space-separated string
snakeCase('some whitespace');
// Returns: 'some_whitespace'
// Convert hyphen-separated string
snakeCase('hyphen-text');
// Returns: 'hyphen_text'
// Handle consecutive uppercase letters
snakeCase('HTTPRequest');
// Returns: 'http_request'
null or undefined are treated as empty strings.
import { snakeCase } from 'es-toolkit/compat';
snakeCase(null); // ''
snakeCase(undefined); // ''
str (string, optional): The string to convert to snake case.(string): Returns the converted string in snake case.