Open In App
Related Articles

How to convert string to camel case in JavaScript ?

Improve Article
Improve
Save Article
Save
Like Article
Like

In this article, we are going to learn how can we convert string to camel case in JavaScript. we will be given a string and we have to convert it into the camel case. 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 str.replace() method

Use the 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 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: 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

Approach 3: Using the Lodash _.camelCase() Method

In this approach, we will use the lodash _.camelCase() method which will conver the given string into the camel case.

Example: This example shows the implementation of the above-explained approach.

Javascript




// Requiring the lodash library
const _ = require('lodash');
 
// Use of _.camelCase() method
let str1 = _.camelCase("Geeks for Geeks");
 
// Printing the output
console.log(str1);
 
// Use of _.camelCase() method
let str2 = _.camelCase("GFG-Geeks");
 
// Printing the output
console.log(str2);


Output:

geeksForGeeks
gfgGeeks

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 29 Nov, 2023
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials