w3resource

JavaScript: Uncapitalize the first character of a string

JavaScript String: Exercise-39 with Solution

Write a JavaScript function to uncapitalize the first character of a string.

Test Data:
console.log(Uncapitalize('Js string exercises'));
"js string exercises"

Visual Presentation:

JavaScript:  Uncapitalize the first character of a string

Sample Solution:

JavaScript Code:

// Define a function named Uncapitalize which takes a string (str1) as input
function Uncapitalize(str1){
  // Return the input string with its first character converted to lowercase,
  // concatenated with the rest of the string starting from the second character
  return str1.charAt(0).toLowerCase() + str1.slice(1);
}
// Call the Uncapitalize function with the input string 'Js string exercises'
// and print the result to the console
console.log(Uncapitalize('Js string exercises'));
 

Output:

js string exercises

Explanation:

This JavaScript code defines a function called "Uncapitalize()" which takes a string 'str1' as input. Inside the function:

  • It uses the "charAt(0)" method to get the first character of the input string.
  • It then converts this first character to lowercase using "toLowerCase()".
  • Finally, it concatenates this lowercase first character with the rest of the string (excluding the first character), achieved by using "slice(1)".

This effectively converts the first character of the input string to lowercase while leaving the rest unchanged.

Flowchart:

Flowchart: JavaScript: Uncapitalize  the first character of a string

Live Demo:

See the Pen JavaScript Uncapitalize the first character of a string-string-ex-39 by w3resource (@w3resource) on CodePen.


Improve this sample solution and post your code through Disqus.

Previous: Write a JavaScript function to create a case-insensitive search.
Next: Write a JavaScript function to Uncapitalize the first letter of each word of a string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/javascript-exercises/javascript-string-exercise-39.php