-4

I'm looking for an elegant way of understanding JavaScript array and objects.

I came to an anomaly in which I got stuck. Since in PHP or other languages, when we make an array e.g

 $a = [
    admin => 1,
    staff => 2
 ];

so if we want to access its element we can do so by for e.g $a[admin] and we will get 1.

similarly if its an object e.g

 $a = (object) [];
 $a->sadd = 'sas';

we can access it with arrow

 $a->sadd

and if we try to access object elements in the style of array i.e like this $a['sadd'] it will throw error that you can not use it as array style.

But I was surprised by the anomaly in JavaScript.

I have observed that in JavaScript no matter what I am making, an array or object, the elements of both can be accessed via dot or via array style and i found no difference in there accessing style.

for e.g

var a = {sadd : 1}

I can access its element via a['sadd'] or a.sadd both will give 1

So I am confused by this anomaly and wondering whether array and object both datatypes are considered same in JavaScript?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Saddam
  • 1,174
  • 7
  • 14
  • 1
    No, they're obviously not the same. They just use similar syntax to access their elements. – Jonathan Hall Jul 26 '20 at 14:48
  • its just how js works.. its a godsend when property names `var a = {123 : 1, 'a b c': '2', 'a-b-c': ''}` as `a.1`, `a.a b c`, `a.a-b-c` obviously wont work – Lawrence Cherone Jul 26 '20 at 14:48
  • 1
    Hi Saddam, Objects and Arrays work same across all the tech stack. You should check Object oriented programming principle and other things. Or best is take a course online for Javascript. That will help you understand Javascript well. Asking such question here will attract more down votes. (PS: I haven't down vote :) ) – Jimmy Jul 26 '20 at 14:49
  • Everything in javascript is an object. Arrays are just specialized objects – charlietfl Jul 26 '20 at 14:54
  • 4
    @Jimmy i think you are wrong objects and arrays do not work same across all tech , and i have provided you the example as well that in php you can not access array elements with same style. – Saddam Jul 26 '20 at 14:57
  • @Jimmy This question has nothing to do with OOP. Also some languages have associative arrays , javascript does not – charlietfl Jul 26 '20 at 15:08
  • 1
    @saddamkamal Please check `https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array` it has few thoughts that you are looking for, – Asutosh Jul 26 '20 at 15:10

2 Answers2

0

An array is indeed an object.

Javascript is a dynamic language and accepts mixed types of entities. Also while accessing, dot notation seems to be more clearer (atleast imo) and is preferred. Bracket notation is used for dyanamic keys.

The difference between array and objects boils down to their usecase: Array -> Contiguous block of memory Object -> key-value pair (like a dictionary)

Shravan Dhar
  • 1,455
  • 10
  • 18
0

Your php example is actually creating what we'd call an object in JS, not an array. In JS an array is a list of items, which you can find items by array[i], or by looping.

An object is a collection of fields, which you can go into by object.fieldName or object[fieldName].

This can be confusing in JS though, because theoretically everything is an "object", including arrays, due to the way things are handled lower down..

I would recommend following along with the https://justjavascript.com/ course for good mental models on how objects work in JS.

Luke Storry
  • 6,032
  • 1
  • 9
  • 22
  • First statement is a bit misleading since there are associative arrays in php. The same structure in javascript is not an array – charlietfl Jul 26 '20 at 14:58