Messages

Home Login Logout

How to replace the First Letter of a String with upper/lower case in JavaScript

How to replace the First Letter of a String with upper/lower case in JavaScript. A small and practical tutorial on building code for this handy little function.

Here is a quick code snippet for the JavaScript version of ucfirst. The code snippet will allow you to capitalize the first letter of any string using JavaScript.

function ucfirst(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}

 

This code snippet will use the JavaScript function charAt to get the character at a certain index.
 
var firstLetterToChange = string.charAt(0);


Next, we use the uppercase function in JavaScript to set this string to be in capitals.

var uppercaseTheFirstLetter = string.charAt(0).toUpperCase();

Then we can add the rest of the string on to this capital letter by using the function slice() which will allow us to get the rest of the string and remove the first letter.

var theStringWithoutFirstLetter = string.slice(1)

Putting this all together, we get the JavaScript ucfirst() alternative.

return string.charAt(0).toUpperCase() + string.slice(1);

At the same manner a quick code snippet for the JavaScript version of lcfirst will allow you to replace the first letter of a string with lower case using JavaScript.

function lcfirst(string) 
{
    return string.charAt(0).toLowerCase() + string.slice(1);
}

Lates posts
Most visited posts

Built with love to give everyone an opportunity to say something to the world
Copyright © 2018 All rights reserved - SITE123 

Terms - Privacy - Abuse

LocalTheme

Help

سوالی دارید؟ با ما مطرح کنید !پشتیبانی آنلاین است.در حال حاضر پشتیبانی غیرفعال است.با تیم ویرا وب صحبت کنیدبا تیم ویرا وب صحبت کنید