DEV Community

Discussion on: Javascript String Methods You Must Know to Become an Algorithms Wizard

 
adam_cyclones profile image
Adam Crockett 🌀

I don't think of JavaScript as a language anymore, it's more of a vendors interpretation of an engine. I appreciate your point of view but honestly, if something behaves like something else who cares if factually not the same.

Thread Thread
 
sleeplessbyte profile image
Derk-Jan Karrenbeld

JavaScript has, as you might be aware, a specification called EcmaScript. The implementation (say v8) is indeed an interpretation of that specification, but this specification does dictate how all this is supposed to work, quite well. In this case, it even states that the specification doesn't specify how strings are supposed to be stored. It also specifically states that the string "elements" are the UTF-16 integers that @dominela10 speaks about.

ref: String Type

However, it also describes how the string iterator is different than the array iterator. To clarify: it specifically states that the string iterator iterates over codepoints, which is the closest thing to a character JavaScript has, which is not the same as a code element. To interpret a string as an array is therefore something that is indeed pretty careless.

ref: String Iterator

Iterators are not Arrays, Arrays are, depending on the language, iterable, or yield an iterator; strings are, depending on the language iterable, or yield an iterator; iterators are iterable.

In that aspect, @adam_cyclones you are correct that the string iterator is the same as an array iterator.