The browser
value and checked on form fields
Reading and writing a field's value: value is a string, checked is for checkboxes.
Why it exists
Everything a person types into a form reaches your program through value and checked. Without them you can't check the data, work out a price, or put a saved draft back into a field. And this is exactly where the most common beginner bugs hide: a number that's really a string, an empty field that turned into zero, and a value read too early.
Where it's used
- Calculators and carts:
Number(qty.value) * price— the quantity from a field. - Checking a form before sending: is the name empty, do the passwords match.
- "Remember me", "I agree to the terms", "Subscribe to the newsletter" checkboxes —
checked. - Restoring a draft:
textarea.value = savedText— put the text back into the field.
Syntax
input.value // a string: what's in the field now
input.value = "text" // write into the field
checkbox.checked // true or false
select.value // the value of the chosen <option>Examples
const age = document.querySelector("#age");
const vip = document.querySelector("#vip");
const years = Number(age.value); // the string, as a number
const isVip = vip.checked; // true or falseconst value = "7"; // this is how it comes from <input type="number">
value + 1; // → "71"
Number(value) + 1; // → 8
"10" < "9"; // → true — strings compare character by character
Number("10") < Number("9"); // → falsefunction readAge(text) {
if (text.trim() === "") return null;
return Number(text);
}
Number(""); // → 0 — an empty field quietly became zero
readAge(""); // → null
readAge(" 3 "); // → 3
Number.isNaN(readAge("three")); // → trueResults after // → aren't just written down — an automatic check runs and verifies them before every site update.
Why it works this way
A field holds what's on the screen — and the screen shows text. So value is always a string: type="number" changes the keyboard and the checks, but not the value's type. An empty field gives "", and Number("") is 0, so check for emptiness before converting.
Reading value returns the string that's in the field right now. A variable keeps a copy of that string, and when the user keeps typing, the variable doesn't change. So read the field when you need the value — for example inside a submit handler, not when the page loads.
For a checkbox (type="checkbox"), the value property isn't "is it ticked" but what gets sent to the server if it is (by default "on"). Whether it's ticked is what checked tells you. For a <select>, value is the value of the chosen <option>.
Writing input.value = "…" changes the field's text, but no input event fires — that event only comes from the user's actions.
Traps
How it differs from…
When to pick something else
When there are many fields and they all go to the server, collecting them with FormData is simpler than reading one by one. And to react to typing as it happens, read event.target.value in an input handler rather than polling the field with a timer.