Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Revisions

5 of 6
added 413 characters in body
Jared Farrish
  • 49.4k
  • 17
  • 100
  • 108

Are Javascript arrays primitives? Strings? Objects?

I've been monkeying with Javascript for several years now (since back in the day before Gmail, Facebook, and YouTube, if you believe such a day existed). Big high-five to Thau.

My Question, as such:

Are arrays merely objects in disguise? Why/Why not? In what way(s) are they (such/not)?

I have always thought of arrays and objects in JS as essentially the same, primarily because accessing them is identical.

var obj = {'I':'me'};
var arr = ['you':'them']; // see edit

EDIT: As Matti pointed out, the proper syntax is... (and I have always found this tiresome syntax)

var arr = new Array();
arr['you'] = 'them';
console.log(obj.I);
console.log(arr.you);
console.log(obj['I']);
console.log(arr['you']);

Am I mislead/mistaken/wrong? What do I need to know about JS literals, primitives, and strings/objects/arrays/etc...?

EDIT In fact, probably a better question:

Are arrays/objects merely strings in disguise? Why/Why not? In what way(s) are they (such/not)?

EDIT

Great responses; more than I hoped for, that's for sure. I can honestly say I learned something from each answer given, and that all attempted to impart knowledge and wisdom.

Each answer gave different pieces of the picture, and not provided the entire picture I was looking for. However, I selected Šime Vidas' answer, since I think it is the clearest, most succinct answer with references to review. Thanks Šime.

All the others that I thought provided valid responses, I provided a vote. There were several that could have easily been selected as the answer, so I hope anyone in the future that comes across this question will read all of the answers, since there is good information available.

Thanks!

Jared

Jared Farrish
  • 49.4k
  • 17
  • 100
  • 108
lang-js

AltStyle によって変換されたページ (->オリジナル) /