javaScript How to convert snake to camel case for a string

In this blog post, We look at how to convert snake/camel case tutorials with examples in javascript or typescript.

Convert snake case to camel case in Javascript and Typescript

The string is a group of characters that contains word(s).

snake case text is a combination of words, separated by an underscore.

For example

helloworld is a _snake case string example.

snake case rules t sentences and words.

  • no spaces or punctuation in between words
  • words are separated by an underscore.
  • these are used in declaring variables and method names in some programming languages
  • the first letter can be either uppercase or lowercase, but the conventional approach is for the first letter to be lowercase.

camel cases are string words in which the first letter is lower case and the middle words are upper case.

example camel case strings are firstName, lastName

Important points about camel case sentences

  • spaces and punctuation are not allowed
  • It will be used in defining variable names in programming languages like java.

camel cases and snake cases are compound words.

Conversion of a camel to a snake is a normal task for developers.

typescript is a superset of javascript, so javascript code works in typescript.

We can do this in many ways, one of the approaches is using map() function, other using regular expression.

Convert Snake case to Came Case using map() method

This approach is using two functions.

  • given snake case string input is first_name
  • split string into words by separator underscore
  • Iterate each word, returns the first word, and from second-string onwards apply firstUppercase function
  • firstUppercase function is to convert the first letter to uppercase and the remaining string is converted to lowercase
let snakeCaseString = "first_name";
const snakeToCamelCase = (sentence) =>
    .map((word, index) => {
      if (word === 0) {
        return part.toLowerCase();
      return firstUppercase(word);
const firstUppercase = (word) =>
  word && word.charAt(0).toUpperCase() + word.slice(1);




Convert Snake case to Came Case Using regular expression

Simply, using regular expression and performance is best compared with the map() function.

  • Given a string snake case string
  • separate words by separator underscore(_) using regular expression make the second-word first character to upper case.
snakeToCamel = (snakeCaseString) =>
  snakeCaseString.replace(/([-_]\w)/g, (g) => g[1].toUpperCase());

Output is


Sum Up

Learned parse camel case to snake case in javascript and typescript.