For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /reference/string/constantCase.md.

constantCase es-toolkit

Converts a string to constant case.

const result = constantCase(str);

Usage

constantCase(str)

Use constantCase when you want to convert a string to constant case. Constant case is a naming convention where all characters are uppercase and words are separated by underscores (_).

import { constantCase } from 'es-toolkit/string';

// Convert various string formats to constant case
constantCase('hello world'); // returns 'HELLO_WORLD'
constantCase('camelCase'); // returns 'CAMEL_CASE'
constantCase('some-kebab-case'); // returns 'SOME_KEBAB_CASE'
constantCase('PascalCase'); // returns 'PASCAL_CASE'
constantCase('snake_case'); // returns 'SNAKE_CASE'

It's a commonly used naming convention when defining constants in JavaScript or other programming languages.

import { constantCase } from 'es-toolkit/string';

// Generate environment variable names
const configKey = 'api base url';
const envVar = constantCase(configKey); // 'API_BASE_URL'

// Generate constant names
const settingName = 'maximum retry count';
const constantName = constantCase(settingName); // 'MAXIMUM_RETRY_COUNT'

It also properly handles strings with spaces or special characters.

import { constantCase } from 'es-toolkit/string';

constantCase('HTTP Request'); // returns 'HTTP_REQUEST'
constantCase('user-agent-string'); // returns 'USER_AGENT_STRING'
constantCase('  multiple   spaces  '); // returns 'MULTIPLE_SPACES'

Parameters

strstringrequired

The string to convert to constant case.

Returns

Returns a new string converted to constant case.

string
Source: es-toolkit

Re-exported verbatim from es-toolkit. Implementation, edge cases, and performance behavior are owned upstream. This page mirrors the documentation at the pinned version; the linked source is authoritative.