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.
Approach 1: 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.
html
< body style = "text-align:center;" > < h1 style = "color:green;" > GeeksforGeeks </ h1 > < h3 > How to convert string to camel case in JavaScript ? </ h3 > < p id = "GFG_UP" style = "font-size: 15px; font-weight: bold;" > </ p > < button onclick = "gfg_Run();" > click here </ button > < p id = "GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;"> </ p > < script > var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var str = 'Click the button to convert to camelCase'; el_up.innerHTML = str; function camelCase(str) { return str.replace(/(?:^\w|[A-Z]|\b\w)/g, function(word, index) { return index == 0 ? word.toLowerCase() : word.toUpperCase(); }).replace(/\s+/g, ''); } function gfg_Run() { el_down.innerHTML = camelCase(str); } </ script > </ body > |
Output:

Convert string to camel case
Example 2: This example uses replace(), toLowerCase(), and toUpperCase() methods to convert a string into camelCase.
html
< body style = "text-align:center;" > < h1 style = "color:green;" > GeeksforGeeks </ h1 > < h3 > How to convert string to camel case in JavaScript ? </ h3 > < p id = "GFG_UP" style = "font-size: 15px; font-weight: bold;" > </ p > < button onclick = "gfg_Run();" > click here </ button > < p id = "GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;"> </ p > < script > var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var str = 'Click the button to convert to camelCase'; el_up.innerHTML = str; 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() { el_down.innerHTML = camelCase(str); } </ script > </ body > |
Output:

Convert string to camel case
Approach 2: 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.
HTML
< body style = "text-align:center;" > < h1 style = "color:green;" > GeeksforGeeks </ h1 > < h3 > How to convert string to camel case in JavaScript ? </ h3 > < p id = "GFG_UP" style = "font-size: 15px; font-weight: bold;" > </ p > < button onclick = "gfg_Run();" > click here </ button > < p id = "GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;"> </ p > < script > var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var str = 'Click the button to convert to camelCase'; el_up.innerHTML = str; function camelCase(str){ // converting all characters to lowercase var ans = str.toLowerCase(); // Returning string to camelcase return ans.split(" ").reduce((s,c)=> s + (c.charAt(0).toUpperCase()+ c.slice(1) )); } function gfg_Run() { el_down.innerHTML = camelCase(str); } </ script > </ body > |
Output:

Convert string to camel case
Please Login to comment...