A FormData object holds a form’s fields as names and values, the way a browser sends a form. get() reads one field’s value by its name. Browsers and Node.js both have FormData.
Every command and output on this page is real, run with Node.js 26.13.1 on macOS.
Syntax
formData.get(name)
formData: aFormDataobject.name: the field’s name, the same as thenameof its input in the form.- It gives back the field’s value, or
nullwhen the form has no field with that name.
See FormData: get() method (opens in a new tab) on MDN.
Prerequisites
- Node.js, to run the example. Install it from the Node.js download page (opens in a new tab).
The Example
A form with one field, email, built in code with append(), which adds a field. Then two reads: the email field and a phone field the form doesn’t have. The file is form.js.
Step 1: Write the Code
const form = new FormData();
form.append("email", "sam@example.com");
console.log(form.get("email"));
console.log(form.get("phone"));
new FormData() creates an empty form, and append() adds the email field.
Step 2: Run It
node form.js
sam@example.com
null
get("email") finds the field. get("phone") gives null, because the form has no phone field.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Expecting empty text for a missing field | You get null | Check for null before you use the value |
| Expecting a number back | Values come back as text: a field set to 30 is "30" | Convert it with Number() |
Using get() for a field with several values | You get only the first one | Use getAll(), which gives all of them in an array |
FAQ
How do I get a FormData from a real form?
In a browser, new FormData(form) collects the fields of a <form> element. See the FormData() constructor (opens in a new tab) on MDN.
Conclusion
formData.get(name) reads a field’s value by its name. Expect text, or null when the field isn’t there.
Additional Resources
- FormData: get() method (opens in a new tab) (MDN)
- FormData: append() method (opens in a new tab) (MDN)
- Run Node.js scripts from the command line (opens in a new tab) (Node.js docs)
JavaScript is a trademark of Oracle Corporation in the United States. Node.js is a trademark of the OpenJS Foundation.
0 comments