JavaScript For Kids For Dummies
Book image
Explore Book Buy On Amazon

The string data type in JavaScript holds text. There are a few cool tricks that you can do with strings besides just storing and printing them.

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.

Getting the length of a string.
Getting the length of a string.

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.

You see the word property used a lot when people talk about JavaScript. A property is something that describes or is a part of something. For example, a car's color is a property of the car, a person's name is a property of the person, and a string's length is a property of the 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

Count the letters in this sentence. There are 10 — 11 if you count the period at the end of the sentence. But when you enter this command into the JavaScript console, you get 14. Do you know why?

The spaces in a string count just as much as the letters, punctuation, symbols, and numbers in the string. To use an analogy, it's all just knots on the string (14 of them to be precise) to JavaScript.

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.

This brings you to a very important concept in JavaScript called zero‐based numbering. Unlike people, who have ten fingers and generally start counting at the number one, JavaScript starts counting at zero. So, in the previous example, when JavaScript wants to tell you that I is the first character in the string, it says that I is at position 0.

If JavaScript were on a sports team, it would proudly wear a shirt that read "We're number 0!"

About This Article

This article is from the book:

About the book authors:

Chris Minnick and Eva Holland are experienced web developers, tech trainers, and coauthors of Coding with JavaScript For Dummies. Together they founded WatzThis?, a company focused on training and course development.

This article can be found in the category: