Coding for Kids

Variable Type: String

A string variable is a text variable. Think of a string as being like a word or a bunch of characters on a necklace—on a string. They are in a specific order and that's that.

Strings are immutable, which means you can't change them. Anything you do to modify a string creates a new string. We won't need to worry about that, but if you wrote a program with massive amounts of work on strings, you could run in to computer memory problems!

Declaring a String

To declare a string, you still use let but you also need quotation marks. You can use single ' or double quotes ", at your preference. I prefer double quotes so that's what I will use.

Make sure that each time you open a quote, you also close it.

Also, don't use smart quotes. Those are the ones that curl in toward the words, “like this”.

Syntax:

Examples:

These, however, would all result in syntax errors:

As Booleans

In JavaScript, you can test strings as Booleans. All strings are evaluated as true, except an empty string, "", which evaluates as false.

Concatenation

Often, you will want to combine strings together to make a sentence you can pass along to a user. This process is called concatenation. It sounds like a lot, but it's just adding. We use a plus sign. It's that easy.

Notice that spaces are not added automatically. The first example has a space inside the quotes after Hi. The last example forces a blank space in there. But the others don't have any extra spaces because I didn't tell them to.

Also notice what's going on with the numbers. Because there is a string in there, it treats everything added after the string as if it's also a string. In the toys example, the parentheses cause the math (2 + 7) to be done first, but the computer then adds from left to right. It sees a string first, so it adds the 3 like a string and then it adds the 9 as a string. It takes some getting used to.

Changing Case

We sometimes need to change a string to UPPERCASE or lowercase. There are methods for doing this easily. We need to make use of a dot operator and we attach it to the string. Ready?

Look how the dot operator is used. After the name of the string variable, you put a dot (period) and then the name of the method you're using on it.

Remember that uppercase letters are considered to be different from lowercase letters. Let's say you ask a user to input a letter as a choice from A, B, C, or D. If they type in lowercase d it would be marked wrong unless you were testing for uppers and lowers. If you're only checking once, no big deal. But to check for all four possibilities, you'd have a lot of extra code to deal with. With smart use of .toUpperCase() and .toLowerCase(), you can make your life easier. Use this to convert the user's answers to lowercase, then just check for those responses.

String Length using .length

It can be important to know the length of a string. Use the .length property on the end of a string. The length is the exact number of characters contained inside the quotes, including spaces and punctuation, etc.

Get one character

JavaScript allows the use of array notation to grab a single character from a string. The thing to remember is that everything is zero-indexed. This means that the first character is index 0. This is considered a non-standard way to do this.

Get Part of a String using .substring(start, [end])

This is considered the proper way to get a character (or several) from a string. You attach this method to the string and you pass it one or two parameters, the start index and (optionally) the ending index. It returns back all the characters from the starting index to the character just before the ending index. Important: This will not change the original string.

Syntax:

The ending index is optional. If you don't give an ending index, it takes the string from the start point to the end of the string. If you need to cut off the first few characters, that's a good way to do it!

Examples:

Find an index in a String using .indexOf(text)

This method allows you to search for specific text inside a string. It can also be used with arrays. Mind the capitalization of the letter O in indexOf. The text has to match exactly in terms of spelling, spacing, and capitalization.

This returns the index of the first instance of a match. It returns -1 if it can't find a match. Remember that indexes start at zero. Let's use our mixedLetters example from earlier.

Examples:

You can also use .lastIndexOf(text) to find—you guessed it—the last occurrence of a section of text.

In both of these cases, you can also include a second parameter that tells the computer where to start the search. This way, if you don't want the first or last occurrence, you can locate an occurrence in the middle somewhere.

Chaining

These methods can be chained together into a more complex-looking type of command. Let's say we want to use our mixedLetters string and see if the word "mix" in it, but we don't care about the capitalization. The long way would be to check for mix, Mix, MIx, MIX, miX, and so on. Crazy. The short way would be to make the string lowercase and just look for mix. We can do this in parts or chain them all together like this.

The methods are processed in order from left to right, so it starts with the string, then it makes it lowercase, then it looks for "mix". This is getting advanced but it's worth seeing early on so you know there's more you can do later when you know what's what.

Escape Character

There are times where you need to put a set of quotes inside a string. But if you're using double quotes and you need the string to show double quotes, then there's a problem.

Consider this:

This would give you an error. If you look at the Wow!, that double quote in front actually closes the first double quote! We may understand it, but the computer won't. You can't even just switch the outer double quotes to singles because of that's, which also has a single quote mark. We can fix this using an escape character, the backslash \.

When the computer sees a backslash in a string, it says, "Oh, I should display the next character, not interpret it." You can use this for double quotes \", for single quotes \', and to show a backslash itself \\.

Now you're a master on string theory!

—Dr. Wolf