How to convert string to camel case in JavaScript ?
Given a string and the task is to convert it into camelCase using JavaScript. In this case, the first character of string converted into lower case and other characters after space will be converted into upper case character.
Approach: Use str.replace() method to replace the first character of string into lower case and other characters after space will be into 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.
<!DOCTYPE html> <html> <head> <title> How to convert string to camel case in JavaScript ? </title> </head> <body style="text-align:center;"> <h1 style="color:green;"> GeeksforGeeks </h1> <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> </html> |
Output:
-
Before clicking the button:
-
After clicking the button:
Example 2: This example uses replace(), toLowerCase() and toUpperCase() methods to convert a string into camelCase.
<!DOCTYPE html> <html> <head> <title> How to convert string to camel case in JavaScript ? </title> </head> <body style="text-align:center;"> <h1 style="color:green;"> GeeksforGeeks </h1> <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> </html> |
Output:
-
Before clicking the button:
-
After clicking the button:
Recommended Posts:
- How to convert hyphens to camel case in JavaScript ?
- Convert string to title case in JavaScript
- Convert string into date using JavaScript
- Convert a string to an integer in JavaScript
- JavaScript | Convert a string to boolean
- How to convert string into float in JavaScript?
- How to convert an object to string using JavaScript?
- How to convert a currency string to a double value with jQuery or Javascript?
- How to Convert JS Object to JSON String in JQuery/Javascript?
- JavaScript | Convert bytes to human-readable string
- Convert comma separated string to array using JavaScript
- How to convert seconds to time string format hh:mm:ss using JavaScript ?
- Switch Case in JavaScript
- Case insensitive search in JavaScript
- Compare the Case Insensitive strings in JavaScript
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



