Loading
Lesson 13
Courses / JavaScript for Complete Beginners
Converting String to Upper or Lower Case in JavaScript

Video Transcript

Welcome to NVK Tech World. In this lesson, we're going to learn about some basic string operations to turn all the characters in the string to lowercase or into uppercase. So let's get started. So, strings as we know are sequences or characters that are enclosed in some quotes, right? Like this guy is a string. And we work with strings before we use the we stored our name before previously in a string. And we said some message to we started a message inside a string. Now, we're going to learn how to do some basic string operations here. And these operations are very basic. Essentially, we're going to take a string, for example, Hello World. And then we're going to turn all the characters that you see in this string into either lowercase or uppercase. So let's start with uppercase. So essentially, you have, for example, this first letter H. In this case, it's in uppercase. But all these other letters from the word Hello, they are in lowercase, right? And the same for the word World here, the first letter W is in uppercase and the remaining letters are in lowercase. Now, suppose we want to turn all of these letters into uppercase, how would we do that? So if you have a string, like so, you can say to uppercase to uppercase is a function, okay, that if you call this function on a string like so, it will take this string and generate a new string with all the letters capitalized. Now notice, if I have a function call, what we have always after the function name, do you remember? That's right, we need the parentheses. In this case, this is a function does not take any arguments. So I leave it blank. So this guy will take this string, generate a new string, all the characters in uppercase. So let's try it out. So we call this function. And then we get this output, okay. So all the letters now in this output string are in uppercase that have all been capitalized. So this is the two uppercase function, okay, on a string. Notice that the first letter upper and first letter case is capitalized, it's following that convention that we learned before. It's the camel case naming convention, where each following letter after the first word has to have the first letter capitalized and all the remaining letters in lower case. So always keep that in mind that in JavaScript, you use this naming convention called camel case. Now that we learned how to do uppercase, how about we do lowercase? Suppose I have the following string. I just have a message saying hi there, okay. Now let's take this guy and convert all the letters to lowercase. In this case, all the letters are in uppercase, but we want them to become lowercase. So we can say to lowercase like so. You put up a dot after the string, okay, the quotes to lowercase following the camel case naming convention. And then you have to pass some arguments to the function to lowercase. But in this case, there are no arguments. So this is left in blank. So let's try it out. As you can see, the output is a new string with all the letters in lowercase. All right. So that is to uppercase and to lowercase, two very basic operations on a string Now let's learn how to do all of this if the string were in a variable. So let's clear everything and let's define a variable. Remember how to do that. Let's start off with cost. And let's give this variable a name. Just call it name like we did before. Remember, we said your names and I'm just going to give a fictitious name here. So suppose you have this person full name here, two words, John someone, you can use any name you want. I'm just making an example. So this john someone string notice it's under quotes, right? It's going to be stored in a variable called name. Now, let's say we want to convert all the characters in the john's name to uppercase. How would you do it? You cannot use the string directly because now the string is inside a variable. And we want to be able to use the variable itself. How would you do it? Can you guess? So all you have to do is say the name of the variable that holds the string in this case, its name, and then do the same thing we did before. Dot two uppercase, for example, if we want to convert all the characters to uppercase, and then that's it. You're going to get a new string with all the letters of the original string capitalized. Now notice if I try to see what's inside the variable name now, it's still the same thing. And why? Like I said before, every time I said what happened here, I said the word new. So remember that that's why I said it so many times. When you do this operation, it creates a new string, okay? It's a new string. It does not modify the original. That's why the variable name still holds the original john someone name, like so. Alright, so the two uppercase and two lowercase, they create, they generate new strings and do not modify the original. Now if you really want to modify the original, what you would have to do is make the variable name changeable. Now because I define it with the word cost, remember that I can only assign a value once. We learned before that if you want to be able to reassign a variable's value more than once, you have to use a different keyword when you declared a variable. That's what keyword let remember. So let's do an example. Let full name, I'm going to create this variable called full name. Now I'm going to use let because I want it to be changeable in the future more than once. And I'm going to call it Anna someone. Okay, now full name. If I do full name, for example, to convert to uppercase, as we saw before, that will generate a new string and the variable itself holding the value Anna someone will remain unchanged. If you really want to change the full name to be all caps, you just need to redefine the variable like so. You say full name. Now how do you define a variable? Again, just put the equal sign to assign a new value to this variable. And then you just say full name dot to uppercase. Okay, note that this full name here on the right hand side is going to refer to the original Anna someone like so. Then it's going to take that generate a new string called with the Anna someone all in capital letters, then it's going to assign that value, this guy to the value to the variable full name. So whatever's in full name will be replaced with Anna someone all capitalized. Okay, so let's see how it goes. Now let's see the value of full name again. Now you can see it changed because we reassigned with the equal operator. Okay, so keep that in mind as you're working with strings, and you have to learn about if the output if some function, for example, to uppercase, you have to ask the question that this does this function modify the original thing. In this case, whatever you it's on the left hand side in this case. So I'll skip that in mind, because there might be functions that modify the original thing. But there are functions that all that might not modify. In this case, we're dealing with two uppercase and two lowercase. And these two functions on the string, do not modify the original. Okay, so keep these two things in mind, doesn't modify it doesn't modify the original as you go through your journey in JavaScript. All right. So this is it for this lesson. Thank you so much for watching. And until the next time. Bye.
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: