Originally published on php-to-js.learnio.dev — part of the PHP to JavaScript course. View this lesson on the original site.
Use === the same way you learned in modern PHP
If you already prefer === in PHP, keep that habit in JavaScript. Loose == exists in both languages and surprises people who expect “same value” without remembering coercion rules.
PHP habit
<?php
var_dump('5' == 5); // true — string coerced to number
var_dump('5' === 5); // false — type and value must match
var_dump(0 == false); // true
var_dump(0 === false); // false
var_dump(null == undefined); // not applicable in PHP
JavaScript equivalent
console.log('5' == 5); // true
console.log('5' === 5); // false
console.log(0 == false); // true
console.log(0 === false); // false
console.log(null == undefined); // true — special case in JS
console.log(null === undefined); // false
| Operator | Coerces types? | Use in new code? |
|---|---|---|
=== / !== |
No | Yes — default |
== / != |
Yes | No — code review should question it |
Traps that only bite with ==
console.log('' == 0); // true
console.log([] == false); // true
console.log('5' == 5); // true
console.log(null == undefined); // true
None of these are true with ===. After the truthy lesson, you can see why: == leans on coercion and truthiness, not plain equality.
PHP and JS agree on the habit
Teams on PHP 8+ and modern JavaScript share the same rule: strict comparison by default, loose only when you document why.
Try it
Use the amber Try it panel below. Click Run.
const pairs = [
['5', 5],
[0, false],
['', 0],
[null, undefined],
];
for (const [a, b] of pairs) {
console.log(JSON.stringify(a), '==', JSON.stringify(b), a == b, '===', a === b);
}
Read the two boolean columns. Your default in new code should match the === column.
From PHP to JavaScript — JavaScript Syntax Foundations for PHP Devs