Originally published on php-to-js.learnio.dev — part of the PHP to JavaScript course. View this lesson on the original site.
Five values you will log every week
PHP’s type helpers (is_string, is_int, is_null) map cleanly to JavaScript once you know the small differences: one number type, and an extra empty value called undefined.
PHP habit: separate scalar types
<?php
$greeting = 'Hello';
$age = 30;
$ratio = 3.14;
$active = true;
$empty = null;
var_dump($greeting); // string
var_dump($age); // int
var_dump($ratio); // float
var_dump($active); // bool
var_dump($empty); // NULL
JavaScript equivalent
const greeting = 'Hello';
const age = 30;
const ratio = 3.14;
const active = true;
const empty = null;
let unset;
console.log(typeof greeting); // 'string'
console.log(typeof age); // 'number' — int and float share one type
console.log(typeof ratio); // 'number'
console.log(typeof active); // 'boolean'
console.log(typeof empty); // 'object' ← historical quirk
console.log(typeof unset); // 'undefined'
| PHP check | JavaScript check | Notes |
|---|---|---|
gettype($x) |
typeof x |
Returns a string name |
is_string($x) |
typeof x === 'string' |
|
is_int($x) || is_float($x) |
typeof x === 'number' |
One numeric type in JS |
is_bool($x) |
typeof x === 'boolean' |
|
is_null($x) |
x === null |
Do not use typeof for null |
| (no equivalent) | x === undefined |
Missing value, not set |
null in PHP vs null and undefined in JS
<?php
// You usually set null deliberately
$record = null;
// Unset variables are errors/notices in strict PHP — not a third value
const record = null; // deliberate empty
let notProvided; // undefined — declared, never assigned
const missing = {};
console.log(missing.email); // undefined — property does not exist
Use null when you mean “empty on purpose”. Treat undefined as “not there yet” (missing argument, missing key, uninitialised let).
Try it
Use the amber Try it panel below. Click Run.
function describe(value) {
if (value === null) return 'null';
if (value === undefined) return 'undefined';
return typeof value;
}
console.log(describe('text'));
console.log(describe(42));
console.log(describe(null));
console.log(describe(undefined));
You are building the same instincts as gettype() / is_*() in PHP, with one extra empty state to recognise.
From PHP to JavaScript — JavaScript Syntax Foundations for PHP Devs