Technical skill practice
JavaScript switch: Match Commands and Make Fall-Through Explicit
Use switch for a discrete value with named cases. A matching case does not automatically stop execution at its end; break, return or another control transfer determines where execution continues.
The task
Route two read-only commands to the same result, handle save separately and reject unrecognized types. The shared cases are deliberate, unlike an accidentally missing break after an action.
Runnable Node 22 example
const assert = require('node:assert/strict');
function route(command) {
let action;
switch (command) {
case 'preview':
case 'inspect': action = 'read'; break;
case 'save': action = 'write'; break;
case 1: action = 'numeric'; break;
default: action = 'unknown';
}
return action;
}
assert.equal(route('preview'), 'read');
assert.equal(route('inspect'), 'read');
assert.equal(route('save'), 'write');
assert.equal(route(1), 'numeric');
assert.equal(route('1'), 'unknown');
console.log(['preview', 'save', '1'].map(route).join(','));
Expected output and explanation
preview reaches the read assignment through the intentionally shared case path. break prevents the save assignment from running afterward. The numeric case accepts 1 but not the string “1,” making the comparison boundary visible.
The default supplies a defined result for an unrecognized command. This example returns an action label rather than performing storage or granting permission; routing to write is not itself an authorization check.
Output from the example
read,write,unknown
Pitfall to check
If cases declare let or const variables with the same name, use case-local braces where separate block scope is required. Also test unknown values: forgetting a default can leave an action unset even when the known cases work.
Questions, answered
Should every switch end with default?
It depends on the contract, but handle or deliberately reject unsupported values. A default can make that behavior explicit rather than silently returning an uninitialized result.
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.