Originally published on php-to-js.learnio.dev — part of the PHP to JavaScript course. View this lesson on the original site.

Arrays are lists — create, read, and update without surprises

PHP arrays double as lists and maps. JavaScript arrays are ordered lists only. Associative data belongs in objects (next lesson).

PHP habit: building and reading a list

<?php
$tags = ['php', 'laravel'];
$tags[] = 'api';
$tags[0] = 'PHP';

echo $tags[1];           // laravel
echo count($tags);       // 3
echo $tags[count($tags) - 1];  // api

if (in_array('api', $tags, true)) {
    echo "has api\n";
}

JavaScript equivalent

const tags = ['php', 'laravel'];
tags.push('api');
tags[0] = 'PHP';

console.log(tags[1]);              // laravel
console.log(tags.length);          // 3
console.log(tags[tags.length - 1]); // api

if (tags.includes('api')) {
  console.log('has api');
}
PHP JavaScript Notes
$arr[] = $x arr.push(x) Append at end
$arr[$i] = $x arr[i] = x Update by index
count($arr) arr.length Length property, not a function
in_array($v, $arr, true) arr.includes(v) Membership check
array_slice($arr, 1, 2) arr.slice(1, 3) End index exclusive in JS
array_merge($a, $b) [...a, ...b] Shallow merge (spread)

const does not freeze the contents

const scores = [8, 9, 10];
scores.push(7);        // fine — const blocks rebinding, not mutation
scores[0] = 10;      // fine
// scores = [];      // TypeError — cannot reassign the binding

Same mental model as a PHP variable that always points at the same array instance while you mutate it.

Safe copies before you pass arrays around

<?php
$original = [1, 2, 3];
$copy = array_merge($original, [4]);
const original = [1, 2, 3];
const copy = [...original, 4];
const clone = [...original];

Spread is shallow — nested arrays or objects inside are still shared by reference.

Try it

Use the amber Try it panel below. Click Run.

const cart = ['book', 'pen'];
cart.push('notebook');
cart[0] = 'Book';

const summary = {
  items: cart.length,
  last: cart[cart.length - 1],
  hasPen: cart.includes('pen'),
};
console.log(cart.join(', '));
console.log(summary);

You should see the updated list and a small summary object. That is the everyday CRUD shape for list data in JS.

From PHP to JavaScript — JavaScript Syntax Foundations for PHP Devs