JavaScript’s Document.prepend() method inserts a set of Node or string objects before the first child of the Document. It is similar to the Element.prepend() method used with Element objects.

Syntax

Document.prepend(parameter1)
Document.prepend(parameter1, parameter2, /* … , */ parameterN)

Parameters

One or more Node or string objects.

Return value

The prepend() method does not have a return value, which means that the method implicitly returns undefined.

Exceptions

HierarchyRequestError DOMException

Thrown when the node cannot be inserted at the specified point in the hierarchy.

Examples

Here is an example of using the JavaScript prepend() method.

Suppose that we should create a new HTML document to which we should add the root element (<html>) with JavaScript.

What should we do?

One option is to create a new document object and the element and use the prepend() method:

let d = new Document();
let html = document.createElement('html');
d.prepend(html);

The reason why our document variable is d and not document is that the latter variable has already been declared.

Now our HTML looks like this:

<html></html>

Browser compatibility

Chrome
Edge
Firefox
Internet Explorer
Opera
Safari
Chrome (Android)
Firefox (Android)
Opera (Android)
Safari (iOS)
Samsung Internet
WebView (Android)
prepend() 54 17 49 41 10 54 49 41 10 6.0 54

See also