JavaScript runs in a host environment. Here the host is Node.js, so complete examples can read local input and print with console.log. There is no browser document or user interface. Start by asking two questions about every expression: what value does it produce, and what type does that value have? A variable binding can later refer to a different type, but the values themselves still have types.
Use const for a binding you will not reassign and let when reassignment is needed. Write a function to name a reusable computation, pass values as arguments, and use return for its result. An if chooses a branch based on truthiness. These mechanics are enough to begin the first build; the supplied export line connects your function to the tests and will be explained in the modules lesson.
const text = "12";
console.log(text + 3); // 123: string concatenation
console.log(Number(text) + 3); // 15: numeric addition
console.log(text === 12); // false: different typesCoercion is an operation, not a promise that the result satisfies your application. Number("") and Number(" ") produce zero. Number("1e2") produces one hundred. Those are language rules, but a configuration count may intentionally accept only digit strings. Validate that spelling before conversion. A regular expression anchored with ^ and $ checks the complete trimmed string; [0-9]+ requires at least one ASCII digit.
Prefer strict equality, ===, when comparing values whose types matter. It does not convert a string to a number. NaN remains a special case: it is not equal to itself. Number.isFinite and Number.isSafeInteger express numeric requirements more clearly than comparing a value to NaN. The Number type has finite precision, so being an integer is weaker than being an exactly representable safe integer.
function defaultCount(value, fallback) {
return value === null || value === undefined ? fallback : value;
}
console.log(defaultCount(0, 9));
console.log(defaultCount(null, 9));
console.log(0 || 9, 0 ?? 9);Truthiness is useful for conditions, but it is broader than missingness. Zero, the empty string, false, null, undefined and NaN are falsy. The nullish operator ?? replaces only null and undefined. A valid zero count must survive the boundary. Objects and arrays are truthy even when empty; do not let their truthiness stand in for type validation.
For the build, draw a small input table before writing code: missing, zero, digit text, invalid text, fraction, infinity and an unsafe integer. Decide which branch handles each row. Then implement parsing as a pipeline with an explicit rejection path. This prevents convenient conversion from quietly becoming an accidental input contract.