One habit
Before you use a value, ask two questions: what type is it, and can it be missing? Most JavaScript bugs in student projects start when code assumes an input is a number, string, or object without checking.
Values have types
JavaScript values can be strings, numbers, booleans, undefined, null, objects, and functions. You meet them when reading form inputs, URL params, localStorage, and API responses.
Use typeof for a first check. Remember that form inputs usually arrive as strings, even when the user typed 499. That matters if you are building a fee splitter, event budget app, or UPI expense tracker.
Check before using
This is the kind of small guard code recruiters like to see. It shows you know what your code expects.
const amountInput = "499";
const nameInput = "Asha";
const amount = Number(amountInput);
if (typeof nameInput !== "string" || nameInput.trim() === "") {
throw new Error("Name is required");
}
if (Number.isNaN(amount) || amount <= 0) {
throw new Error("Amount must be a positive number");
}
console.log(`${nameInput.trim()} paid ₹${amount}`);Common trap
Do not trust truthy and falsy checks for everything. They are useful, but they can hide valid values.
For example, 0 is falsy. If your app allows a zero balance, if (!balance) will treat it like a missing value. Be specific when the value matters.
const balance = 0;
// Too broad: treats 0 as missing
if (!balance) {
console.log("Balance missing");
}
// Better: checks only for no value
if (balance === null || balance === undefined) {
console.log("Balance missing");
}Safe conversion flow
Do each one yourself, then tap it to tick it off. The ticks are only a checklist for you: they are not marked or scored.
0 of 5 done
Use AI carefully
You can ask ChatGPT, Gemini, or Claude to write validation checks for a function. Paste the function and say what inputs are allowed.
Then check the answer yourself. Look for vague checks like if (value), missing Number.isNaN, and errors that do not tell you what went wrong.
Small reusable check
You do not need a library for basic validation. A clear helper function is enough for many hackathon projects.
function getPositiveNumber(value, fieldName) {
if (value === null || value === undefined) {
throw new Error(`${fieldName} is required`);
}
const numberValue = Number(value);
if (Number.isNaN(numberValue) || numberValue <= 0) {
throw new Error(`${fieldName} must be a positive number`);
}
return numberValue;
}
const ticketPrice = getPositiveNumber("149", "Ticket price");
console.log(ticketPrice + 1);Do this now
Do each one yourself, then tap it to tick it off. The ticks are only a checklist for you: they are not marked or scored.
0 of 5 done