Javascript capitalize first letter

Asked
Active3 hr before
Viewed126 times

7 Answers

capitalizejavascriptletterfirst
90%

At this point we have two variables local to our capitalize function - firstLetter and restOfWord, firstLetter is, well, the first letter of the string argument that was passed into capitalize

Example_snippet/controller/utility/_capitalize.js/ const capitalize = ([firstLett. . .
const capitalize = ([firstLetter, ...restOfWord]) => firstLetter.toUpperCase() + restOfWord.join("");
load more v
88%

Unfortunately in JavaScript, there isn't a capitalize or title case a string, So what we can is utilize toLowerCase() to make the entire string lower-cased and then uppercase the first letter

Example_snippet/controller/utility/_capitalize.js/ function capitalize(word) { . . .
function capitalize(word) {
   const lower = str.toLowerCase();
   return str.charAt(0).toUpperCase() + lower.slice(1);
}
load more v
72%

It works by splitting the string into two pieces, On the first line it pulls out firstLetter and then on the second line it capitalises firstLetter by calling firstLetter

Example_snippet/controller/utility/_capitalize.js/ function capitalizeFirstLetter. . .
function capitalizeFirstLetter(string) {
   return string.charAt(0).toUpperCase() + string.slice(1);
}

console.log(capitalizeFirstLetter('foo')); // Foo
load more v
65%

There are number of ways to capitalize the first letter of the string in JavaScript, Following are the ways:,How to make first letter of a string uppercase in JavaScript ?,How to calculate the number of days between two dates in javascript?,JavaScript | Split a string with multiple separators

Example_snippet/controller/utility/_capitalize.js/ string.toUpperCase() . . .
string.toUpperCase()
load more v
75%

One of the most common operations with strings is to make the string capitalized: uppercase its first letter, and leave the rest of the string as-is,,The best way to do this is through a combination of two functions

Example_snippet/controller/utility/_capitalize.js/ const name = 'flavio' const na. . .
const name = 'flavio'
const nameCapitalized = name.charAt(0).toUpperCase() + name.slice(1)
load more v
40%

The string's first character is extracted using charAt() method, Here, str

Example_snippet/controller/utility/_capitalize.js/ // program to convert first le. . .
// program to convert first letter of a string to uppercase
function capitalizeFirstLetter(str) {

   // converting first letter to uppercase
   const capitalized = str.charAt(0).toUpperCase() + str.slice(1);

   return capitalized;
}

// take input
const string = prompt('Enter a string: ');

const result = capitalizeFirstLetter(string);

console.log(result);
Step 2 continued with Enter a string: javaScript Jav. . .
Enter a string: javaScript
JavaScript
Step 3 continued with str.str[0]; // j. . .
str.str[0]; // j
Step 4 continued with // program to convert first le. . .
// program to convert first letter of a string to uppercase
function capitalizeFirstLetter(str) {

   // converting first letter to uppercase
   const capitalized = str.replace(/^./, str[0].toUpperCase());

   return capitalized;
}

// take input
const string = prompt('Enter a string: ');

const result = capitalizeFirstLetter(string);

console.log(result);
Step 5 continued with Enter a string: javaScript Jav. . .
Enter a string: javaScript
JavaScript
load more v
22%

Next, we use the uppercase function in JavaScript to set this string to be in capitals,,Here is a quick code snippet for the JavaScript version of ucfirst

Example_snippet/controller/utility/_javascript.js/ function jsUcfirst(string) { . . .
function jsUcfirst(string) {
   return string.charAt(0).toUpperCase() + string.slice(1);
}
load more v