'Associative array' is very much a PHP thing. The syntax that you're using is simply object access.
This:
const foo = { prop1: 'value1', prop2: 'value2' };
console.log(foo.prop1]);
Is the same as:
const foo = { prop1: 'value1', prop2: 'value2' };
console.log(foo['prop1']);
The benefit of the second syntax is that you can use other expressions to access the property.
const propName = 'prop1';
console.log(foo[propName]);
You are right that that's somewhat similar to PHP's associative arrays, but there's also obvious differences. One of them is that you can count PHP's associative arrays:
echo count($arr);
But you can't treat Javascript's objects the same. This won't work:
console.log(arr.length);
With javascript you can't also not really loop through a mixture of properties and array values. This is possible in PHP, but not javascript:
$arr = [ 'prop' => 'value', 5];
foreac($arr as $item) echo $item; // outputs 'value' and 5
In javascript, this might look like this:
const arr = [5];
arr.prop = 'value';
But you'll need 2 distinct loops to go through the array values and it's properties:
for(const item of arr) console.log(item);
for(const prop of Object.values(arr)) console.log(prop);
Lastly, a big difference is that Javascript's arrays are dense, and PHP's associative arrays are sparse. PHP's arrays really behave more like hashmaps than arrays. PHP's arrays keep track of their count, and can have both members that have an integer and a string index.
The closest thing Javascript actually has is new Map()
. PHP doesn't have a 'true' array like Javacript does. (depending on your definition of array)