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.
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.