Technical skill practice

JavaScript Contains: Use includes for Text or Array Membership

The standard operation for these JavaScript membership checks is includes, rather than a general contains method. Decide whether the input is text or an array: a substring check and an element check answer different questions.

The task

Test a status label for a substring, then inspect membership in an ID list and an object list. The assertions make type and identity boundaries visible.

Runnable Node 22 example

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

const status = 'Review pending';
assert.equal(status.includes('pending'), true);
assert.equal(status.includes('review'), false);
assert.equal([4, 9].includes(4), true);
assert.equal([4, 9].includes('4'), false);
assert.equal([NaN].includes(NaN), true);
const item = {id: 4};
assert.equal([item].includes(item), true);
assert.equal([item].includes({id: 4}), false);
console.log(`substring=${status.includes('pending')}; numeric=${[4, 9].includes(4)}`);

Expected output and explanation

The text check searches inside the label and distinguishes Review from review. The array check compares individual values, so numeric 4 does not match the string “4.” Deciding to normalize case or convert types should be an explicit input policy.

The object assertions use the same reference first, then a freshly constructed object with equal-looking fields. Only the original reference is a member. Use some or findIndex when your definition of membership is a condition on record fields. The NaN assertion shows that includes can find a NaN element.

Output from the example

substring=true; numeric=true

Pitfall to check

A successful substring check is not full input validation: “not-approved” contains “approved.” Choose equality, a prefix check or a parser when the accepted form is narrower than occurrence anywhere in a string.

Questions, answered

Can I use includes to get the matching position?

It returns a Boolean. Use indexOf for exact primitive positions or findIndex for the first record satisfying a predicate.

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.