One cool string trick is to count how many characters the string is made up of. The way you do that is to use .length after the string, or after a variable holding the string.
For example, to find out the length of the string held inside the book variable, type book.length into the console. The console responds right away with a number, as shown here.
Every string, even an empty string, has a length. The length of an empty string, of course, is 0. Because it’s something that describes a string, you call length a property of a string.
In addition to finding out the length of a string stored in a variable, you can also just attach the length property to a string in quotes to find out its length:
"I am a string.".length
In addition to properties, strings also have things that they can do, or that can be done to them. In programming, you call these things that can be done with or to something its methods.
The most commonly used string method is indexOf. The job of indexOf is to look at your string, find a certain character or group of characters inside it, and tell you what position they’re at. In the following statement, you’re looking for the position of the word am in a string:
"I am a string.".indexOf("am");
When you run this statement in the console, the result is 2. Try retyping the command, but this time look for the capital I.
"I am a string.".indexOf("I");
The result is 0.