Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Practice forEach in JavaScript Practice forEach Challenge 6 Solution

Array method on the string.

So are you saying that we can call array index on the string? When I checked in the console I did:

let color = #F1111;
console.log(color[1]);

And I got back 'F'. Which means we can use bracket notation to access particular character of the string, even though string is not an array. How crazy is that? It gets even better:

let color = '#F1111';
let colors = ['#F1111', '#F2222', '#F3333'];
console.log(colors[1][1]);
F

Which means every simple, basic array is in fact two dimentional array (from the point of view of the each character, if I may think like that). Eureka!

2 Answers

Steven Parker
Steven Parker
215,939 Points

Some string operations are similar to array operations, but they are not the same thing. Using a bracket on a string is essentially the same thing as using the "charAt" method (which is not an array method).

So "colors[1][1]" could be re-written as "colors[1].charAt(1)", since "colors" is an array but "colors[1]" is a string.

But you're right that you can treat an array of strings as a two-dimensional array of characters, just bear in mind that unless all strings are the same size it is a "jagged" array.

Thank you for this clarification.