Spot the String Index Bug
Find the Mistake Easy
Question
This code should get the first character but gets undefined. What is wrong?
Code
const name = "Ada";
console.log(name[3]);
Hint
Strings are zero-indexed, just like arrays.
Explanation
Strings are indexed starting at 0. "Ada" has characters at indices 0 (A), 1 (d), 2 (a). Index 3 is out of bounds and returns undefined. To get the first character, use name[0] which gives "A".
Related Resources
Related Lessons
- Strings and Text
Learn how strings and text work in programming.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.