Search⌘ K
AI Features

Strings Indexing, Length, and Traversal

Explore how to work with strings in JavaScript by learning indexing, length measurement, and traversal methods. This lesson helps you access characters, understand valid indices, and iterate through strings to solve real-world problems using efficient string manipulation.

By now, you know that a string is an ordered sequence of characters stored contiguously in memory and that its structure is essentially identical to an array. It is a clean and simple idea, but an idea alone does not help you solve problems. The real question is how you actually work with a string once you have one. How do you reach into it and pull out a specific character? How do you know how many characters it contains? How do you move through it one step at a time? These are the basic operations that every string algorithm depends on, and getting comfortable with them is what turns the conceptual picture of strings into something you can actually use.

Indexing in strings

Each character in the string is associated with a specific position called an index. Indexing starts at zero, meaning the first character is at index 0, the second is at index 1, the third is at index 2, and so on. This is the same zero-based indexing that arrays use.

Accessing individual characters

Because a string is stored like an array, accessing an individual character works exactly the same way as accessing an element in an array. To retrieve a character at a specific index, write the name of the string followed by the index in square brackets.

Javascript (babel-node)
let s = "hello";
console.log(s[0]); // h
console.log(s[1]); // e
console.log(s[2]); // l
console.log(s[3]); // l
console.log(s[4]); // o

The result of indexing into a string is always a single character. This operation takes constant time O(1)O(1) regardless of which index you use because the computer can calculate the exact memory address of any character directly from its index, just as it does with arrays.

Negative indexing

JavaScript does not support negative indexing with square brackets like Python.

Python offers something that lower-level languages like C do not: the ability to index from the end of a string using negative numbers. An index of -1 refers to the last character, -2 refers to the second-to-last, and so on.

Javascript (babel-node-es2024)
let s = "hello";
console.log(s.at(-1)); // o
console.log(s.at(-2)); // l
console.log(s.at(-3)); // l
console.log(s.at(-4)); // e
console.log(s.at(-5)); // h

Negative indexing is not just a syntactic convenience. It is genuinely useful in practice. Many string problems require looking at the last character or working backward from the end, and negative indices make that code cleaner and easier to read. The alternative, writing s[s.length - 1] every time you need the last character, is verbose and harder to follow at a glance.

The .length property used here returns the total number of characters in a string, which is covered properly in later sections.

It helps to think of positive and negative indices as two different ways of referring to the same positions in a string. For a string of length n, the character at positive index i is the same as the character at negative index i - n.

String: h e l l o
Positive: 0 1 2 3 4
Negative: -5 -4 -3 -2 -1

Both rows describe the same five positions. JavaScript’s .at() method simply gives you a choice of which direction to count from.

Now, take a look at the following visualizer to better understand string indexing.

Length of a string

The length of a string is the total number of characters it contains. In JavaScript, the length is stored when the string is created, and ...