Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
In ECMAScript 5 (and in many recent browser
implementations—including IE8—prior to ECMAScript 5), strings behave like
read-only arrays. Instead of accessing individual characters with the
charAt() method, you can
use square brackets:
var s = test; s.charAt(0) // => "t" s[1] // => "e"
The typeof operator still returns
“string” for strings, of course, and the Array.isArray() method returns false if you pass it a string.
The primary benefit of indexable strings is simply that we can
replace calls to charAt() with square
brackets, which are more concise and readable. The fact that strings
behave like arrays also means, however, that we can apply generic array
methods to them. For example: