Course contentsShow
← Web Development
Lesson 887 of 2,98216. Forms and ValidationFree lesson

Reading FormData Values

Using get(), getAll(), and has() methods to retrieve values from FormData objects.

Reading FormData Values

You'll learn how to extract and inspect individual values stored in a FormData object using built-in retrieval methods.

What Are These Retrieval Methods?

Once you've created a FormData object (either from an existing form or manually), you need ways to read what's inside it. The FormData API provides three key methods for this:

  • get(name) — retrieves the first value associated with a given field name
  • getAll(name) — retrieves all values for a field name (useful for multi-select inputs or checkboxes with the same name)
  • has(name) — checks whether a field exists in the FormData, returning true or false

Why Do We Need Different Methods?

Think of FormData like a filing cabinet. Sometimes a drawer (field name) contains just one document (get()), sometimes it contains multiple copies (getAll()), and sometimes you just want to check if a drawer exists at all (has()).

Using the Methods

const formData = new FormData();
formData.append('username', 'alice');
formData.append('role', 'admin');
formData.append('role', 'editor'); // Same name, multiple values

// Get first value
const username = formData.get('username'); // "alice"
const firstRole = formData.get('role');    // "admin" (only first value)

// Get all values
const allRoles = formData.getAll('role');  // ["admin", "editor"]

// Check if field exists
const hasEmail = formData.has('email');    // false
const hasUsername = formData.has('username'); // true

Notice that get() only returns the first value when multiple exist, while getAll() returns an array of all values.

Key Takeaway: Use get() for single-value fields, getAll() for fields with multiple values (like checkboxes), and has() to check for field existence before attempting retrieval.