Thursday, October 16, 2025
HomeLanguagesJavascriptHow to convert string to camel case in JavaScript ?

How to convert string to camel case in JavaScript ?

In this article, given a string and the task is to convert it into camelCase using JavaScript. In this case, the first character of the string is converted into lowercase, and other characters after space will be converted into uppercase characters.

Approaches to convert string to camel case:

Approach 1: Using the JavaScript str.replace() method

Use 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

Approach 2: Using JavaScript 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 1: 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

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS