Originally published on php-to-js.learnio.dev — part of the PHP to JavaScript course. View this lesson on the original site.
Pick const first, let when the value must change
You already declare variables all day in PHP. JavaScript does the same job with different syntax: no $, and a keyword that signals whether the name can be reassigned.
PHP habit
<?php
$name = 'Alice';
$count = 0;
$count = 1;
// One syntax for everything — readers cannot see reassignment intent from the keyword
JavaScript equivalent
const name = 'Alice'; // default: this name will not point elsewhere
let count = 0; // this name will change
count = 1;
// name = 'Bob'; // TypeError: assignment to constant variable
| PHP | JavaScript | What changes |
|---|---|---|
$name = 'Alice' |
const name = 'Alice' |
Express “won’t rebind” at declaration |
$count = 0; $count++ |
let count = 0; count++ |
Express “will rebind” with let |
| No direct equivalent | var x = 1 |
Legacy; avoid in new code |
Mapping: const is not final on objects
In PHP you can still mutate an array or object held in a variable:
<?php
$user = ['name' => 'Alice'];
$user['name'] = 'Bob'; // fine
// $user = []; // also fine in PHP — full reassignment allowed
const user = { name: 'Alice' };
user.name = 'Bob'; // fine — mutating the object
// user = {}; // TypeError — cannot rebind the const
const in JavaScript is closer to “this variable name always refers to this binding” than to freezing the whole structure.
Why var bites PHP developers
PHP’s $ in a loop body does not leak the way var leaks across a function:
<?php
for ($i = 0; $i < 3; $i++) {
// $i is scoped to the loop in practice for most scripts
}
for (var i = 0; i < 3; i++) {
// i still exists here — function scope, not block scope
}
console.log(i); // 3 — often a surprise
for (let j = 0; j < 3; j++) {
// block-scoped — closer to what you expect from modern PHP style guides
}
// console.log(j); // ReferenceError
flowchart LR
PHP["PHP: one `$` syntax"] --> JSconst["JS: const by default"]
JSconst --> JSlet["let when reassignment needed"]
JSlet --> Avoid["never var in new code"]
Try it
Use the amber Try it panel below. Click Run.
const role = 'PHP developer';
let lessonsCompleted = 0;
lessonsCompleted += 1;
console.log(role, lessonsCompleted);
Same mental model as PHP variables, with stricter signals in the keywords.
From PHP to JavaScript — JavaScript Syntax Foundations for PHP Devs