When .index and .length Team Up — It Gets Confusing, Right?
When .index and .length Team Up — It Gets Confusing, Right?

.length shows the total number of characters, and .index tells you the position of each one, starting from 0. Just remember—length is about “how many,” and index is about “which one.”
But when .length and .index appear together—just like in the example above—it can start to get confusing.
“Wait, exactly where does it stop cutting?” It’s easy to feel overwhelmed.
That’s okay.💜
Let’s take it step by step, like baby steps, and understand it together—one small piece at a time.
Step 1. Think of a string as a line of characters standing in a row
Let’s take the word “HELLO” as an example.
It’s made up of five letters: “H”, “E”, “L”, “L”, and “O”, all standing neatly in a row.
Each letter takes up one spot, lining up one by one in order—just like friends waiting patiently in line.
Step 2. The index is the position number of each character (starting from 0)
Character | H | E | L | L | O |
---|---|---|---|---|---|
index | 0 | 1 | 2 | 3 | 4 |
The index starts from 0.
While we usually count from 1, computers have a habit of starting from 0 instead.
So when we talk about an index, we’re using the numbering system that computers understand.
That’s why in the word “HELLO”,
“H” is at index 0, and “O” is at index 4.
Even though “H” is the first letter to us, in programming, its index is 0.
It might feel a little strange at first, but don’t worry—once you get used to it, it actually makes things a lot easier.
Step 3. length means the number of characters (how many there are)
character | H | E | L | L | O |
---|---|---|---|---|---|
length | 1 | 2 | 3 | 4 | 5 |
How many characters are in “HELLO”?
That’s right—5 characters!
So, the .length is 5.
Character | H | E | L | L | O |
---|---|---|---|---|---|
length | 1 | 2 | 3 | 4 | 5 |
index | 0 | 1 | 2 | 3 | 4 |
This might feel a bit tricky at first:
The .length of “HELLO” is 5, but since .index begins at 0,
the last letter “O” is at index 4 (not 5).
Just subtract 1 from the length, and you’ll find the final index!
The index of the last character = (length – 1)
Once you separate the idea of how many (length) from where (index),
all the tricky parts will begin to feel much clearer!
Step 4. substring is a tool for cutting ✂️
It cuts from the start and stops just before the end.
This code starts at index 0 and slices up to, but not including, index 2.
Character | H | E | L | L | O |
---|---|---|---|---|---|
index | 0 | 1 | 2 | 3 | 4 |
substring(0, 2) gives you “HE”It includes index 0 and index 1, but not index 2.
In other words, the starting index is included,
but the ending index is not.
It might feel a bit confusing at first,
but if you just remember this one rule—
“The end index is not included in .substring()”—you’ll be just fine.
Practice : “HELLO”.substring(1, 4)
Step 5. str2.substring(0, str.length – 3)
Let’s take a closer look—what is this code really doing?
str.length is 5 because “HELLO” has 5 characters.
So, str.length – 3 is 5 – 3 = 2
That means we’re keeping the part of “HELLO” before the last 3 letters,
which is “HE” — since the last 3 letters “LLO” are left out.
(HELLO – LLO = HE)
So by using substring(0, 2),
we’re starting from the beginning and simply taking the part before the last 3 letters.
In other words, we’re keeping just the front part, and leaving out the last 3 characters.
Character | W | O | R | L | D |
---|---|---|---|---|---|
index | 0 | 1 | 2 | 3 | 4 |
str2.substring(0, 2) means we’re cutting “WORLD” from index 0 up to, but not including, index 2.
In other words, we’re taking the first two letters of “WORLD”,
which gives us “WO”.
Practice : “HELLO”.substring(0, “HELLO”.length – 4)
Things to Remember
Concept | Meaning | Example |
---|---|---|
length | The total number of characters in a string | “HELLO”.length = 5 |
index | The position of a character (starting from 0) | “L” is at index 2 |
substring(a, b) |
|
substring(1, 4) → “ELL” |