Whenever a program deals with text — a name typed into a form, a password, a
tweet, the title of a song, a line of a poem — it is working with a
Take the word "HELLO". It isn't one lump — it's the character H,
then E, then L, then L, then O, in that
exact order. Because a string is an ordered sequence, we can ask sensible questions
about it: how long is it? what's the third character? what are the first two letters? can I
stick two strings together? This page is a tour of the everyday tools every programmer uses
to answer questions like those.
.length
Every string knows its own size. Ask for its .length and you get back the
number of characters it contains — letters, digits, spaces and punctuation all
count. Press Run:
Notice that .length has no brackets after it — it's a property the
string carries, not something you call. This one little number turns out to be the key that
unlocks everything else on this page, because it tells us how far we can count.
Each character sits at a numbered position called its index, and here is the
rule that trips up almost every beginner: counting starts at 0, not 1. So the
first character is at index s[i] — to pull out that one character. Study the diagram, then run the code:
Look carefully at that last line. The word has length
Indexing starts at 0. So for a string s:
s[0] (not s[1]);s[s.length - 1] (not
s[s.length]).
That last one is the trap. It feels natural to write s[s.length] to grab the last
character — but "STRING" has length undefined, and this off-by-one error — being one position out at
the very end — is one of the most common bugs in all of programming. Whenever you touch the
last character of a string, reach for s.length - 1.
.slice and .substring
Often you don't want a single character but a chunk of the string — the first
three letters, the middle bit, everything after the space. That's called a substring,
and you take one with .slice(start, end). It gives you the characters from index
start up to but not including end — the same "stops just
before" habit you saw with indexing.
.substring(start, end) does almost exactly the same job and you'll see it a lot too;
for everyday, forwards slices the two behave identically. The handy trick to remember is
slice(start, end) returns s.slice(4, 7) gives
Gluing strings end-to-end is called concatenation, and you do it with the very
same + sign you use to add numbers. With text, though, + doesn't add —
it joins. This is how programs build up messages, greet users by name, and stitch
together output:
That final line is worth pausing on: "1" + "1" is "11", not
.toUpperCase() and .toLowerCase()
Two more tools you'll use constantly flip a string's case:
.toUpperCase() SHOUTS it, .toLowerCase() whispers it. These do
have brackets, because you are asking the string to do something and hand back a new
string (the original is never changed).
That last example is the everyday reason to change case: if a quiz should accept
"Paris", "PARIS" and "paris" as the same answer, you
lower-case both sides before comparing. Case-changing turns messy human input into a
tidy, comparable form.
Because a string is a sequence with a known .length, you can visit
every character in turn with a
i < word.length — that runs i across exactly the valid indices
Once you can walk through the characters, you can process them. Here we count how many
times the letter "s" appears in a sentence — a running total that goes up by one
each time the loop lands on an s:
These few tools combine into genuinely useful little programs. Here we build someone's initials by grabbing the first character of each name and upper-casing it:
To reverse a word, loop through it backwards — start at the last index
word.length - 1 and count down to
And a first taste of validation — checking a password is long enough. Here we
simply compare its .length against a minimum:
Yes — and forgetting it causes real bugs. The string "Hi there" has length
It looks like a mistake, but it's deliberate and clever. Deep down, a string is stored in the
computer's memory as a row of slots, and the index is really "how far along from the start am
I?" The first character is right at the start — zero steps along — so it's index