The browser
FormData
An object with all of a form's fields: name–value pairs, just as they'd be sent.
Why it exists
A sign-up form can have twenty fields. Reading each one with querySelector(...).value is long-winded, and it's easy to forget one. FormData takes the whole form and decides what goes in by the same rules the browser uses when submitting: fields with a name, only ticked checkboxes, the chosen option of a list. You can pass it straight to fetch — files included.
Where it's used
- A submit handler:
Object.fromEntries(new FormData(form))— every field in one line. - Groups of checkboxes with one name: chosen tags, sizes, awards —
data.getAll("tags"). - File uploads: an avatar or a document goes to the server along with the other fields.
- Frameworks: form handlers in React and Next.js receive their data as
FormData.
Syntax
const data = new FormData(form);
data.get("name"); // a string or null
data.getAll("tags"); // an array of strings
data.has("vip"); // true or false
Object.fromEntries(data) // a plain objectExamples
const data = new FormData(); // in a browser: new FormData(form)
data.append("name", "Whiskers");
data.append("tags", "cute");
data.append("tags", "fluffy");
data.get("name"); // → "Whiskers"
data.getAll("tags"); // → ["cute", "fluffy"]
data.get("vip"); // → nullconst data = new FormData();
data.append("age", "3");
data.append("vaccinated", "on"); // a ticked checkbox without a value
const cat = {
age: Number(data.get("age")),
vaccinated: data.has("vaccinated"),
vip: data.has("vip"),
};
cat; // → { age: 3, vaccinated: true, vip: false }hasconst data = new FormData();
data.append("name", "Mittens");
data.append("age", "2");
Object.fromEntries(data); // → { name: "Mittens", age: "2" }
[...data.keys()]; // → ["name", "age"]Object.fromEntries and keys()Results after // → aren't just written down — an automatic check runs and verifies them before every site update.
Why it works this way
new FormData(form) walks over the form's fields and adds a name–value pair for each one that has a name and isn't disabled. A checkbox or radio button is included only if it's checked; its value is the value attribute, or the string "on" if there isn't one. A <select> gives the value of the chosen option.
Inside, it's an ordered list of pairs, not an object: one name can appear several times. That's why there's get (the first value, or null) and getAll (every value as an array). Values are always strings (or files): FormData knows nothing about numbers or true/false.
FormData iterates like an array of [name, value] pairs, so for...of, Object.fromEntries(data) and new URLSearchParams(data) all work.
Traps
How it differs from…
valueandcheckedon form fieldsinput.value reads one field and gives you full control.
FormDatareads the whole form by the submission rules. For two fieldsvalueis enough; for a whole formFormDatais handier.
When to pick something else
When you need one or two fields, or a reaction to every keystroke — just read input.value in the handler. And when the data doesn't look like form fields (nested objects, arrays of objects), build a plain object and send it as JSON.