JavaScript document body Property Tutorial

In this section, we will learn what the document body property is and how to use it in JavaScript.

What is document body Property in JavaScript?

The JavaScript document body property is used to get a reference to the <body> element in the current document.

This property could also be used to replace the current content of the target <body> element on a page as well.

document body Property Syntax:

document.body;

document.body = newValue;

document body Property Input Value

The value we set for this property will be used as the replacement of the current content of the <body> element.

document body Property Return Value

The return value of this property is a reference to the <body> element in the page. So that means, using this property, we can access the child, sub-children, and other related elements (like parent elements) of the <body> in an HTML document.

Example: using document body Property in JavaScript

See the Pen using document body Property in JavaScript by Omid Dehghan (@odchan1) on CodePen.

In this example, we’ve used the `body` property to access the <body> element in the page and changed its color to lightblue after 3 seconds delay. Also, after 5 seconds delay, the content of the body is changed to “Hello Wordl :)”.

Note: you can learn more about the setTimeout() function in the related section.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies