ExampleConvert to lowercase: Show
let text = "Hello World!"; Try it Yourself » Definition and UsageThe The SyntaxParametersReturn Value
Browser Support
ES1 (JavaScript 1997) is fully supported in all browsers:
This article explains how to convert a string to lowercase and uppercase characters. We'll also go over how to make only the first letter in a word uppercase and how to make the first letter of every word in a sentence uppercase. Let's get started! The The general syntax for the method looks like this:
The Strings in JavaScript are immutable. The It means that the old, original string is not changed or affected in any way.
The string Any letters that are already
lowercase are not affected by the The string in the example below consists of all capital letters. They are all then converted to lowercase when the
How to use the toUpperCase() method in JavaScriptThe The general syntax for calling the method looks like this:
It doesn't take in any parameters. As strings in JavaScript are immutable, the It instead returns a new value. The string specified is converted to a new one whose contents consist of only all uppercase letters. This means that there will now be two strings: the original and the newly converted capitalized one.
Any capital letters already
in the string will not be affected and will remain unchanged when the How to capitalize only the first letter in a string in JavaScriptWhat if you want to make only the first letter of a string a capital? Below is a simple example that shows you one way to do just that. Say there is a variable called
You first locate and extract the first letter of that string by using its index. Then you call the As a reminder, indexing in JavaScript (and most programming languages) starts at Save this operation in a new variable called
Next, you want to isolate and cut off that first character and keep the remainder of the string. One way to do this is by using the
You want to start from the second letter until the end of the value. In this case, the argument you should pass to This way, the first character is excluded altogether. A new string is returned without it but containing the rest of the characters – minus that first letter. Then save that operation to a new variable.
By combining the two new variables with concatenation, you get a new string with only the first letter capitalized.
Another way is to combine the steps from above and isolate them in a function. The function gets created just once. The function then returns a new string with the first letter capitalized. The amount of code you need to write is substantially less while also being able to pass in any string as an argument without writing repetitive code.
How to capitalize the first letter of every word in JavaScriptBut how do you make the first letter of every word in a sentence uppercase? The method shown in the section above won't work as it doesn't deal with multiple words, just a single word in a sentence. Say you have a sentence like the one below. You want to capitalize every first word in the sentence.
The first step is to split the sentence into individual words and work with each one separately. For that, you use the It splits the sentence based on blank spaces. Create a new variable and store the new array.
Now from that sentence, there is a new array of words that allows you to manipulate each word on its own, separately. Since there is now a new array, you can use the In the
The first letter of every word is now capitalized. All that is left now is to combine the words in the array together in a single sentence again. For that you use the
As shown in the section above, you can also create a function that combines all theses steps. You will then be able to pass as an argument any string and each first word in it will be uppercase.
ConclusionAnd there you have it! This is how you use the You learned how to capitalize the first letter of a word and capitalize the first letter of each word in a sentence. If you want to learn JavaScript and gain a better understanding of the language, freeCodeCamp has a free JavaScript Certification. You'll start from the basics as an absolute beginner to the language and then advance to more complex subjects such as Object Oriented Programming, Functional Programming, Data Structures, Algorithms, and helpful Debugging techniques. In the end, you'll build five projects to put your skills to practice. Thanks for reading, and happy learning! Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started What method returns the list of the words in a string?Method#1: Using split() method
The built-in method returns a list of the words in the string, using the “delimiter” as the delimiter string.
What is the return value of the string method Lstrip ()?Return Value from lstrip()
lstrip() returns a copy of the string with leading characters stripped. All combinations of characters in the chars argument are removed from the left of the string until the first mismatch.
Which method can be used to return a string in upper case letters quizlet?In Python, the capitalize() method converts the first character of a string to capital (uppercase) letter. If the string has its first character as capital, then it returns the original string.
Which method would you use to determine whether a substring is the suffix of a string?The endsWith() method determines whether a string ends with the characters of a specified string, returning true or false as appropriate.
|