Subscripts in Practice
You use subscripts to reference elements inside an ordered collection — arrays, lists, strings, matrices, whatever you're working with. The syntax depends entirely on the language, and mixing them up is the fastest way to introduce a bug into your code.In Swift, a subscript is its own feature. You can define custom ones on classes, structs, and enums. It's not limited to one-dimensional indexing. I remember spending an afternoon debugging a crossword puzzle solver where I'd written a two-dimensional lookup that looked correct on paper but silently returned nil because I'd reversed the row and column parameters. The compiler doesn't warn you about parameter ordering unless you name them explicitly. Naming them fixed it immediately. JavaScript uses bracket notation: array[0] or object["key"]. That's about all there is to it for built-in types. Python adds slice syntax on top, so lst[1:5:2] gives you every other element from index 1 through 4. Ruby lets you define [] and [] = methods on any class, which works similarly to Swift's approach but with less type safety. SQL has its own quirks. Most dialects don't support true array subscripts natively. You end up using JSON_EXTRACT or string splitting functions depending on your database. PostgreSQL got jsonb subscript operators a while back now, which is genuinely useful if you're working with structured JSON data.
Common Pitfalls
Off-by-one errors are the classic problem. Some languages use zero-based indexing, some use one-based. Lua, MATLAB, and R start at 1. Python, C, Java, and JavaScript start at 0. If you're switching between them regularly, your muscle memory will fight you. Out-of-bounds access behaves differently everywhere. JavaScript returns undefined. Swift throws a runtime crash. Python raises an IndexError. C and C++ give you undefined behavior, which is the worst possible outcome because the program might appear to work correctly until it doesn't, and then debugging becomes a nightmare. Another thing people overlook: subscripts aren't always read-only. In Swift, if you don't provide a setter, the subscript is read-only by default. I've seen developers write what they thought was a normal assignment and wonder why the compiler complained. Same situation exists in some form across multiple languages. Always check whether your subscript supports writing.
Performance Note
For most everyday code, subscript performance isn't a concern. But if you're doing tight loops over large datasets in a language like Python, repeated list lookups can add up. Using a local variable instead of re-accessing a deeply nested subscript repeatedly inside a loop cuts execution time noticeably. The difference isn't dramatic for small lists, but on arrays with millions of elements in a CPU-bound loop, it becomes measurable. In Swift, computed subscripts on value types can trigger expensive copies if you're not careful. I ran into this when building a matrix type where each subscript access was creating a copy of an entire row. Switching to an in-place mutation approach and using inout parameters cleaned it up. It also made the code easier to reason about because the intent was explicit.
Get the Full Details

When Subscripts Aren't the Right Tool
Not everything should be accessed with bracket notation. If you're building a public API and the access pattern involves validation, side effects, or complex logic, a regular method is often clearer than a subscript. Swift's own standard library leans heavily on subscripts, but that's because the types are simple and well-scoped. For domain models with business rules, methods communicate intent better. There's also the readability question. data[1][2][3] is fine for a few levels, but once you're chaining five or six subscripts together, it becomes unclear what each index represents. Naming the intermediate values or wrapping the access in a method call makes the code self-documenting without extra effort. That's basically how subscripts work in the wild. Pick the right syntax for your language, watch your indexing base, and don't overuse them where methods would serve you better.