Technical skill practice

typeof in JavaScript: Inspect Values and Handle Null and Arrays

typeof is useful for a first inspection, but it is not a complete data-shape validator. In particular, null reports “object” and arrays also report “object.” Add a test that matches the distinction you actually need.

The task

Classify a short mixed-value list and then explicitly identify its array and null entries. The example avoids treating every object-like result as an editable record.

Runnable Node 22 example

const assert = require('node:assert/strict');

const values = [17, '17', false, undefined, null, [], () => 1];
const types = values.map(value => typeof value);
assert.deepEqual(types, ['number', 'string', 'boolean', 'undefined', 'object', 'object', 'function']);
assert.equal(values[4] === null, true);
assert.equal(Array.isArray(values[5]), true);
assert.equal(typeof NaN, 'number');
console.log(types.join(','));

Expected output and explanation

The numeric-looking string remains a string; typeof does not convert it. The null and array entries share the word object but differ under the explicit tests. A function has its own typeof result, while NaN still belongs to the number type.

Choose the next check from the contract. Array.isArray answers whether a value is an array; equality with null answers whether that particular empty marker is present. Neither establishes all the fields or invariants of a business record. An input validator may need several checks rather than one typeof expression.

Output from the example

number,string,boolean,undefined,object,object,function

Pitfall to check

An object result does not make property access safe on null. Guard it before reading fields. Also distinguish an unknown identifier from a known property whose value is undefined when writing diagnostics; the dedicated undefined check covers that boundary.

Questions, answered

Does typeof prove a number is finite?

No. NaN and Infinity both have the number type. Number.isFinite is a separate check for finite numeric values.

Sources and examples

ResumizeAI team. Examples are fictional teaching material, not customer results. Replace sample details with your own facts.

Your experience. A complete resume.

Build in Resumize with ATS-friendly templates, guided editing and a live preview. Start free, then upgrade when you need more.

Free account required. Free-plan PDF exports include a watermark.