0
var str = 'foobar';
console.log( str[4] );

prints: 
a

This proves that string also acts like an array.Am i correct?

Maizere Pathak.Nepal
  • 2,383
  • 3
  • 27
  • 41
  • 1
    Define "acts like an array". To what extend does a string need to "act like an array" for that statement to be considered true? – deceze Apr 12 '13 at 06:27
  • I don't know about "proof", but String is spec'd to support array-like (read-only) access to the characters: https://developer.mozilla.org/en/docs/JavaScript/Reference/Global_Objects/String – Thilo Apr 12 '13 at 06:27
  • possible [duplicate](http://stackoverflow.com/questions/5943726/string-charatx-or-stringx) – Artyom Neustroev Apr 12 '13 at 06:27

1 Answers1

3

You can't set characters at a specific index, so no, not really:

> var a = 'foo';
undefined
> a[0] = 'x';  // No warning, no error. It just silently fails.
"x"
> a
"foo"
Blender
  • 289,723
  • 53
  • 439
  • 496
  • +1. And this is also mentioned in the spec (https://developer.mozilla.org/en/docs/JavaScript/Reference/Global_Objects/String): "For character access using bracket notation, attempting to delete or assign a value to these properties will not succeed." – Thilo Apr 12 '13 at 06:29