Saturday, November 16, 2024
Google search engine
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 Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments