Skip to main content
0 reactions

How to Read a Form Field with FormData.get() in JavaScript

A card titled FormData.get(), labelled JavaScript Basics, showing get(“email”) giving sam@example.com and get(“phone”) giving null.
FormData.get(): Read a form field’s value by its name.

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

SyntaxJavaScript
formData.get(name)
  • formData: a FormData object.
  • name: the field’s name, the same as the name of its input in the form.
  • It gives back the field’s value, or null when the form has no field with that name.

See FormData: get() method (opens in a new tab) on MDN.

Prerequisites

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

form.jsJavaScript
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

TerminalBash
node form.js
OutputPlain text
sam@example.com
null

get("email") finds the field. get("phone") gives null, because the form has no phone field.

Common Mistakes

MistakeWhat happensDo this instead
Expecting empty text for a missing fieldYou get nullCheck for null before you use the value
Expecting a number backValues come back as text: a field set to 30 is "30"Convert it with Number()
Using get() for a field with several valuesYou get only the first oneUse 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

JavaScript is a trademark of Oracle Corporation in the United States. Node.js is a trademark of the OpenJS Foundation.

0 comments