Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
When we want to deal with text in our programs, characters, words, sentences, email addresses, we use the term strings to describe those kind of values. Now we've seen these already. Anytime you see words contained in sets of quotes, we're dealing with strings. The first line of code that we wrote was this alert message that used the string "Hello, world". This value contained in the double- quotes is referred to as a string literal, just like the number 5 or the number 1,000,000 would be a numeric literal.
We can of course use this format to create variables. I could create a new variable called message and set it equal to the words "Hello, world". I could then use that variable and pop out another alert box. So we are using the string literal to create the variable called message and then writing that variable out. Now, notice that I'm not using double- quotes around message in this second alert statement because I don't want to write out the word message. I want to write out the value of the variable called message.
Let's say for example we want a phrase that contains both double quotes and single quotes. Well, there is no simple way to mark out the beginning and the end of this without doing something else here. If this is what we need, what we can do is what's called escaping the quotes. This would be the way that I'd have to write it. The entire string begins and ends with double quotes and that means if I want double quotes inside that string, I mark them by putting a backslash before the double quotes being used inside the body of the string itself.
Now, one of the great benefits of dealing with strings in most programming languages is they are smart. These variables go beyond just having a holding place for some characters. We can ask things of them. We'll get into this a lot later, but just to give you a basic example, let's say I create a new variable called phrase and set it to the words "This is a simple phrase." Well, one of the things I can do is I can use the name of that variable to access or get to information about it, such as how long is it.
I can also ask questions of it, like does another word exist inside it? I can convert it to upper or lowercase. As the most straightforward example here, I am going to write the line alert and then instead of just the name phrase, I am going to say phrase.length. This is allowing me to get to what is called the length property of this variable, and what's going to happen is this will pop up an alert box that will say in this case 24. We can actually ask questions of our string variables.
As we get deeper into creating and working with any language, you will find this is very common, but variables by themselves aren't dumb. They can actually give us more information about the contents and about what they're holding. Now we'll get deeper into strings a little later on, but that's enough to get us started.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 99857 Viewers
56 Video lessons · 112982 Viewers
71 Video lessons · 81811 Viewers
131 Video lessons · 39261 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.