Languages
[Edit]
EN

JavaScript - make lower case only for first letter of text

5 points
Created by:
Root-ssh
175460

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:

// ONLINE-RUNNER:browser;

function decapitalizeTest(text) {
    return text && text[0].toLowerCase() + text.slice(1) || text;
}

// Usage examples:

console.log(decapitalizeTest('Abc'));                     // abc
console.log(decapitalizeTest('John said: This is text')); // john said: This is text

ES6:

// ONLINE-RUNNER:browser;

const decapitalizeTest = (text) => {
    return text && text[0].toLowerCase() + text.slice(1) || text;
};

// Usage examples:

console.log(decapitalizeTest('Abc'));                     // abc
console.log(decapitalizeTest('John said: This is text')); // john said: This is text

Naive solution:

// ONLINE-RUNNER:browser;

console.log('Abc'.toLowerCase());                     // abc
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.

Alternative titles

  1. JavaScript - decapitalize text
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join