JS Built-ins
Algorithmsintermediate

CamelCase to Snake Case

πŸ“– Usage Example
function toSnakeCase(s) {
  return s.replace(/([A-Z])/g, '_$1').toLowerCase();
}
toSnakeCase('camelCaseString'); // 'camel_case_string'

Convert camelCase to snake_case by inserting underscores before capital letters.

CamelCase to Snake Case

Convert camelCase strings to snake_case.

"helloWorld" β†’ "hello_world"
"firstName" β†’ "first_name"
"myVariableName" β†’ "my_variable_name"

Algorithm:

  1. Find uppercase letters
  2. Replace each with underscore + lowercase version
function toSnakeCase(s: string): string {
  // Convert camelCase to snake_case
}

toSnakeCase('helloWorld') // "hello_world"
πŸ’‘ Hints (4)
  • β€’ Use replace() with a regex pattern.
  • β€’ Pattern /([A-Z])/g matches uppercase letters.
  • β€’ The replacement function receives the match (m).
  • β€’ Return "_" + m.toLowerCase() to insert underscore and lowercase.