Related Articles

Related Articles

How to convert string to camel case in JavaScript ?
  • Last Updated : 21 Jun, 2019

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>                    

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>                    

chevron_right


Output:

  • Before clicking the button:
  • After clicking the button:

full-stack-img




My Personal Notes arrow_drop_up
Recommended Articles
Page :