Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Practice forEach in JavaScript Practice forEach Challenge 6 Solution

Piotr Manczak
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Piotr Manczak
Front End Web Development Techdegree Graduate 29,363 Points

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
231,269 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.