Check out a free preview of the full JavaScript: From First Steps to Professional course
The "Strings" Lesson is part of the full, JavaScript: From First Steps to Professional course featured in this preview video. Here's what you'd learn in this lesson:
Anjana discusses the data type string, what strings are composed of, how to determine the length of a string, and demonstrates how characters are organized in a string.
Transcript from the "Strings" Lesson
[00:00:00]
>> All right, so strings, as we have already seen show up all the time in JavaScript. In our JavaScript lives, we will run into strings a lot. And it's important to know how to work with them and some of the tricks they can do. So, let's talk about working with strings.
[00:00:14]
Or again, as I like to think of them, the friendship bracelets of JavaScript. [LAUGH] So what are strings made up of? When you're trying to put together one of these little friendship bracelets you might have a bunch of beads each with a letter on them. In JavaScript, we have strings that are made up of smaller units of text.
[00:00:36]
Anybody know what we call these tiny little units of text that usually correspond to something like a letter? Any idea what we call these? So we call these little units or these little letter beads.
>> Characters.
>> Characters, thank you Chat, beautiful. So characters are what strings are made of.
[00:00:57]
Each string is made up of zero or more character units. And we could think of these as the little beads that we could string together to spell out a word or a whole sentence or whatever else. Maybe it's just emoji, who knows? So characters are the kind of individual units that make up a string.
[00:01:23]
So earlier when we had the .length that we saw returned, we saw type of some string .length as a number. The thing that that number to is the number of characters in the string. So in this case, for example, maybe the second example is easier to find. So what do you think super in quotes .length is going to return?
[00:01:50]
>> Five.
>> Five, cuz we can count that. Let's just confirm. Okay, now, how about supercalifragilisticexpialidocious .length? Does anybody off the top of their heads know how to count a string that long? No, not me. Let's find out, how long is this super long string? I didn't copy.
[00:02:11]
Let's find out. Okay, it's not working. Let's type it out, fragilisticexpialidocious. 34 characters that's too long, it's too many characters that's why I couldn't even handle it [LAUGH]. [LAUGH] So when we wanna find out how many characters we're working with in a particular string, we can use the .length property to tell us.
[00:02:40]
And, well, just another note, let's say we had a string like before. I had some string .length. If I type that in, I get 11. So some four letters, string as five letter six letters. Well, where's that extra letter coming from?
>> Space.
>> The space. The space itself is a character.
[00:03:03]
So I can even have a string of just a space, and that is still a valid character. Space is as much a thing as a letter is. We can also have a string with no characters, which is called the empty string. And if I asked for the length of that it will tell me 0, still a string.
[00:03:26]
Even though it has nothing inside of it, it's still a value of typestring which I can prove to myself with typeof. So, characters are the units from which strings are made up. And in each string, characters are in a specific order, right? It would be kind of chaos if they weren't, if every word or sentence that we tried to type was just like whatever order you want, a jumble of beads.
[00:03:54]
When we string together these beads, these little characters we want them to go in a specific order, and so each character gets a number. So, if we have our string ALOHA, Like on that little letter bead. We're imagining this on like a little beaded bracelet or something like that.
[00:04:22]
Each of these characters in our string gets assigned a number. And we start in JavaScript at zero. So some people will say it looks like this, you have each character. In this case, I'm drawing them like little beads just to indicate that there are individual units. Some people would say, so we have Let's say the number 0 assigned to the A, the number 1, L, 2, 3, 4.
[00:04:52]
That essentially each bead kind of gets its own number. This is a totally valid way to think about it. I would say another way that we could think about it, that might come in handy later in our JavaScript lives is to think about 0 as being kind of the point at which the string starts.
[00:05:15]
And 1 at being the point, one character later, and so on and so forth. So 2, 3, 4 and then at the end of the string would come a five, and if we ask this, hey, how long is this string? It'll say ALOHA has five characters, it's five characters long.
[00:05:40]
And if we asked it, what is the character at position one? Let's say, it would tell me which character starts at that number, at that one. So in this case, that would be L, exactly. So these are two different ways that you can think about the numbers assigned to each character.
[00:06:04]
One is that each kind of chunk gets its own number. And the other is that the numbers actually kind of appear in between the chunks in between the characters. And they are an indicator of which character starts there, if that makes sense. Later on in life, we might find that having that second mental representation is helpful when we're talking about substrings or smaller strings within the string.
[00:06:41]
But for now, either way, whichever way you find more useful to think about it, both of them are valid.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops