Languages
[Edit]
EN

JavaScript - how to make lower case only for first letter of text

5 points
Created by:
Root-ssh
6675

In this short article we would like to show how to make lower case for first text letter using JavaScript - how to decapitalise 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.

Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.