FormData
Creates a new FormData object optionally pre-populated with the key/value pairs from an HTML form
Syntax
new FormData(form?, submitter?)Parameters
| Parameter | Type | Description |
|---|---|---|
| form | HTMLFormElement | An HTML form element to populate the FormData from |
| submitter | HTMLElement | A submit button element |
Return Value
A new FormData object
Examples
const formData = new FormData()
formData.append('username', 'alice')
formData.append('email', '[email protected]')
await fetch('/api/register', { method: 'POST', body: formData })const form = document.querySelector('form') as HTMLFormElement
const data = new FormData(form)
console.log(data.get('name'))function objectToFormData(obj: Record<string, string>) {
const fd = new FormData()
Object.entries(obj).forEach(([k, v]) => fd.append(k, v))
return fd
}Understanding FormData
The FormData method in JavaScript creates a new FormData object optionally pre-populated with the key/value pairs from an HTML form. It belongs to the FormData object and is one of the most widely used methods for working with formdata values in modern JavaScript and TypeScript applications.
The method signature is new FormData(form?, submitter?). It accepts 2 parameters: form, submitter. When called, it returns a new formdata object. Understanding when and how to use FormData() helps you write more expressive, readable code.
Common use cases for FormData include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like fetch-formdata-append, fetch-formdata-get, fetch-response-formdata, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for FormData is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
FormData.prototype.appendAppends a new value onto an existing key inside a FormData object, or adds the key if it does not already exist
FormData.prototype.getReturns the first value associated with a given key from within a FormData object
Response.prototype.formDataTakes a Response stream and reads it to completion, returning the result as a FormData object
More FormData Methods
Other methods in the FormData object
Related Tools
More FormData Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.