Technical skill practice

JavaScript startsWith: Check a Prefix at the Intended Position

startsWith checks whether text begins with a specified prefix at a given position. It is case-sensitive and narrower than asking whether a substring appears anywhere. A successful prefix check is not a complete format validation.

The task

Inspect a fictional dispatch reference and the same reference inside a labeled message. Then compare a string that merely contains the prefix later.

Runnable Node 22 example

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

const reference = 'JOB-314';
assert.equal(reference.startsWith('JOB-'), true);
assert.equal(reference.startsWith('job-'), false);
assert.equal('Ref: JOB-314'.startsWith('JOB-', 5), true);
assert.equal('old-JOB-314'.startsWith('JOB-'), false);
assert.equal('old-JOB-314'.includes('JOB-'), true);
assert.equal('JOB-'.startsWith('JOB-'), true);
console.log(`prefix=${reference.startsWith('JOB-')}; nested=${'old-JOB-314'.startsWith('JOB-')}`);

Expected output and explanation

The offset five points immediately after “Ref: ” in the labeled message. The old-reference example contains the marker but fails the start check, which shows why includes would accept a broader set of inputs.

The bare JOB- string also passes the prefix test even though it has no number. If the complete reference requires digits, a length or grammar check must enforce that part. Prefix checking alone cannot prove the suffix is present or valid.

Output from the example

prefix=true; nested=false

Pitfall to check

Do not silently lowercase a case-sensitive reference before checking it. Decide whether the format permits that normalization. Position is a string offset, not a record field number, so confirm it against the actual input layout.

Questions, answered

Can startsWith accept a regular expression?

It is a prefix-string operation, not a regex matcher. Use a deliberate pattern operation when the required grammar calls for one.

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.