5/7 I Don't Get It!


For 5/7 I don't understand how the variable text is an array. Is there something I missed? Thanks! (BTW I passed this level, I just don't understand how it works.) :grin:


text is not an array but a string. In simple terms you can iterate over a string just as you would an array


Allow me to explain. A variable is more technically known as an identifier. I like to call them name values when I refer to them in my notes. I do so because the javascript data value for them is read as syntax of javascript and their data type is not a string. There is actually a particular function which turns a string into this identifier. Without the function, it's nearly impossible to bridge the line between identifier and string.

A variable holds a single value of data, however a variable holding a string, is really just holding an array of values concatenated as a string. It's very simplified for us users, but it's essentially an array at this point. You can test this out and you don't even have to do anything fancy at all!

var string = "This string ain't what it seems."; //Declare out string variable
string[3]; //Now use array syntax to call an index within the string
//You should get the letter S

even though this is a completely normal variable, and we've done nothing to it, we can use the same kind of selector syntax as an array to target an index within the string of a variable. Now I may have completely turn your world upside down but it's okay, don't panic! This is just how Javascript handles information, and it's actually quite beautiful to know this because you can trust Javascript all the more than you probably realized.

Now the same rule does not apply for integers, because it's a different datatype, but understanding that this is what javascript does with strings is incredibly important. This is why a string must be within quotes so that javascript can handle the information properly. This is also why you don't have to parse each letter of a string every single time you want to search it, or run checks against it. Normally you'd have to construct all of that logic on your own as you do with any normal loop, however javascript takes care of that.


Wait, so you mean that you can use strings as you would arrays, except it doesn't call the whole word, just the letter? Ohhhhhhhhhhhhhhhhh. That makes a lot of sense. :grinning: