EN
JavaScript - make lower case only for first letter of text
5 points
In this short article, we would like to show how to make the lower case for the first text letter using JavaScript - how to decapitalize text.
Quick solution:
xxxxxxxxxx
1
function decapitalizeTest(text) {
2
return text && text[0].toLowerCase() + text.slice(1) || text;
3
}
4
5
// Usage examples:
6
7
console.log(decapitalizeTest('Abc')); // abc
8
console.log(decapitalizeTest('John said: This is text')); // john said: This is text
ES6:
xxxxxxxxxx
1
const decapitalizeTest = (text) => {
2
return text && text[0].toLowerCase() + text.slice(1) || text;
3
};
4
5
// Usage examples:
6
7
console.log(decapitalizeTest('Abc')); // abc
8
console.log(decapitalizeTest('John said: This is text')); // john said: This is text
Naive solution:
xxxxxxxxxx
1
console.log('Abc'.toLowerCase()); // abc
2
console.log('John said: this is text'.toLowerCase()); // john said: this is text
Note: in above naive solution we believe that
toLowerCase()
method will solve the problem because text is simple.