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
let str = 'Click the button to convert to camelCase' ;
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() {
console.log(camelCase(str));
}
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
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) {
let ans = str.toLowerCase();
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
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
const _ = require( 'lodash' );
let str1 = _.camelCase( "Geeks for Geeks" );
console.log(str1);
let str2 = _.camelCase( "GFG-Geeks" );
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