Decapitalize First Letter in JavaScript

JavaScriptJavaScriptBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will explore how to decapitalize the first letter of a string in JavaScript using the decapitalize function. This function makes use of array destructuring and string manipulation methods to change the case of the first letter of a string. Additionally, we will see how to optionally convert the rest of the string to uppercase.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript(("`JavaScript`")) -.-> javascript/AdvancedConceptsGroup(["`Advanced Concepts`"]) javascript/BasicConceptsGroup -.-> javascript/variables("`Variables`") javascript/BasicConceptsGroup -.-> javascript/data_types("`Data Types`") javascript/BasicConceptsGroup -.-> javascript/arith_ops("`Arithmetic Operators`") javascript/BasicConceptsGroup -.-> javascript/comp_ops("`Comparison Operators`") javascript/AdvancedConceptsGroup -.-> javascript/spread_rest("`Spread and Rest Operators`") subgraph Lab Skills javascript/variables -.-> lab-28258{{"`Decapitalize First Letter in JavaScript`"}} javascript/data_types -.-> lab-28258{{"`Decapitalize First Letter in JavaScript`"}} javascript/arith_ops -.-> lab-28258{{"`Decapitalize First Letter in JavaScript`"}} javascript/comp_ops -.-> lab-28258{{"`Decapitalize First Letter in JavaScript`"}} javascript/spread_rest -.-> lab-28258{{"`Decapitalize First Letter in JavaScript`"}} end

Javascript Function to Decapitalize String

To decapitalize the first letter of a string, use the following JavaScript function:

const decapitalize = ([first, ...rest], upperRest = false) => {
  return (
    first.toLowerCase() +
    (upperRest ? rest.join("").toUpperCase() : rest.join(""))
  );
};

To use this function, open the Terminal/SSH and type node. Then, call the decapitalize function, passing in the string you want to decapitalize as the first argument.

Optionally, you can set the second argument upperRest to true to convert the rest of the string to uppercase. If upperRest is not provided, it defaults to false.

Here are some examples:

decapitalize("FooBar"); // 'fooBar'
decapitalize("FooBar", true); // 'fOOBAR'

Summary

Congratulations! You have completed the Decapitalize String lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like