Open In App

How to convert string to camel case in JavaScript ?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

We will be given a string and we have to convert it into the camel case. In this case, the first character of the string is converted into lowercase, and other characters after space will be converted into uppercase characters. These camel case strings are used in creating a variable that has meaning.

Example of converting string to camel case in JavaScript

1. Using the str.replace() method

Use the str.replace() method to replace the first character of the string in lower case and other characters after space will be in upper case. The toUpperCase() and toLowerCase() methods are used to convert the string character into upper case and lower case respectively.

Example 1: This example uses RegExp, toLowerCase(), and toUpperCase() methods to convert a string into camelCase. 

Javascript
// Input string with spaces
let str = 'Click the button to convert to camelCase';

// Function to convert into camel Case
function camelCase(str) {
    // Using replace method with regEx
    return str.replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) {
        return index == 0 ? word.toLowerCase() : word.toUpperCase();
    }).replace(/\s+/g, '');
}

// To display output
function gfg_Run() {
    console.log(camelCase(str));
}
// Function call
gfg_Run()

Output
clickTheButtonToConvertToCamelCase

Example 2: This example uses replace(), toLowerCase(), and toUpperCase() methods to convert a string into camelCase. 

Javascript
let str = 'Click the button to convert to camelCase';

function camelCase(str) {
    return str
        .replace(/\s(.)/g, function (a) {
            return a.toUpperCase();
        })
        .replace(/\s/g, '')
        .replace(/^(.)/, function (b) {
            return b.toLowerCase();
        });
}

function gfg_Run() {
    console.log(camelCase(str));
}
gfg_Run()

Output
clickTheButtonToConvertToCamelCase

2.Using reduce() and split() method

Use reduce() method to iterate over the character of the string and convert it into camel case. The toUpperCase() and toLowerCase() methods are used to convert the string character into upper case and lower case respectively.

Example: This example uses reduce, toLowerCase(), and toUpperCase() methods to convert a string into camelCase. 

Javascript
let str = 'Click the button to convert to camelCase';

function camelCase(str) {
    // converting all characters to lowercase
    let ans = str.toLowerCase();

    // Returning string to camelcase
    return ans.split(" ").reduce((s, c) => s
        + (c.charAt(0).toUpperCase() + c.slice(1)));

}

function gfg_Run() {
    console.log(camelCase(str));
}
gfg_Run()

Output
clickTheButtonToConvertToCamelcase

3.Using the Lodash _.camelCase() Method

In this approach, we will use the lodash _.camelCase() method which will conver the given string into the camel case.

Example: This example shows the implementation of the above-explained approach.

Javascript
// Requiring the lodash library 
const _ = require('lodash');

// Use of _.camelCase() method
let str1 = _.camelCase("Geeks for Geeks");

// Printing the output 
console.log(str1);

// Use of _.camelCase() method
let str2 = _.camelCase("GFG-Geeks");

// Printing the output 
console.log(str2);

Output:

geeksForGeeks
gfgGeeks

4.Using Array.map() and Array.join()

In this approach first we split the string at hyphens (-) . Then using map() method we will iterate over each word in the array of substrings. For the first word (at index 0), no changes are made, For subsequent words, we will capitalized the first character and then concatenate with the rest of the word.

Example: This example shows the implementation of the above-explained approach.

JavaScript
function toCamelCase(str) {
    return str
        .split(/[-_]/)
        .map((word, index) => {
            if (index === 0) {
                return word;
            }
            return (
                word.charAt(0).toUpperCase() +
                word.slice(1)
            );
        })
        .join("");
}

console.log(toCamelCase("this_is_camel_case"));

Output
thisIsCamelCase


Last Updated : 18 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads