Element.prototype.prepend
Inserts a set of Node objects or strings before the first child of the Element
Syntax
element.prepend(...nodes)Parameters
| Parameter | Type | Description |
|---|---|---|
| nodes | ...(Node | string)[] | A set of Node or string objects to insert |
Return Value
undefined
Examples
const list = document.querySelector('ul')!
const li = document.createElement('li')
li.textContent = 'First item'
list.prepend(li)const container = document.getElementById('messages')!
container.prepend('New message: ', document.createElement('br'))function prependNotice(parent: HTMLElement, text: string) {
const notice = document.createElement('div')
notice.className = 'notice'
notice.textContent = text
parent.prepend(notice)
}Understanding Element.prototype.prepend
The Element.prototype.prepend method in JavaScript inserts a set of Node objects or strings before the first child of the Element. It belongs to the Element object and is one of the most widely used methods for working with element values in modern JavaScript and TypeScript applications.
The method signature is element.prepend(...nodes). It accepts 1 parameter: nodes. When called, it returns undefined. Understanding when and how to use prepend() helps you write more expressive, readable code.
Common use cases for Element.prototype.prepend include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like dom-append, dom-before, dom-insertbefore, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for Element.prototype.prepend 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
Element.prototype.appendInserts a set of Node objects or strings after the last child of the Element
Element.prototype.beforeInserts a set of Node or string objects in the children list of this Element's parent, just before this Element
Element.prototype.insertBeforeInserts a node before a reference node as a child of a specified parent node
Element.prototype.appendChildAdds a node to the end of the list of children of a specified parent node
More Element Methods
Other methods in the Element object
Related Tools
More Element Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.