HTML - DOM Element toString() Method



The toString() method allows us to convert an HTML element into a string format without changing the original element. It usually shows its type as [object Object].

Syntax

object.toString();

Parameters

This method does not accepts any parameters.

Return Value

The toString() method does not directly return a value. Instead, it is automatically used to convert an object into a string format.

Examples of HTML DOM Element 'toString()' Method

Below are some of the examples of toString() method, which converts an object into a string format for displaying or performing different operations in HTML DOM.

Using toString() Method on an HTML Element

This example shows the usage of the toString method. The code accesses the content of a

element by calling the toString() function, which converts the element to its string format and updates the content to display the result of toString().



 
    toString() Method Example



  

HTML - DOM Element

toString() Method

Click the button to get the result!

Example content.

Converting an Array to String

This example includes an array containing elements. The toString() method is applied to convert the array into a string. The code then updates the content of a

element to display the array as a string.



 
    toString() Example: Array



  

HTML - DOM Element

toString() Method

Converting an Array to String

Converting Number to String

This example shows the usage of the toString() method to convert a number into a string. The code then calls the toString() method and updates the content of a

element to display the number as a string.



 
    Converting Number



  

HTML - DOM Element

toString() Method

Converting Number to String

Converting Object to String

This example shows the usage of the toString() method to convert an object with properties 'name' and 'age' into string . The code then calls the toString() method and updates the content of a

to display the object as string.



 
    Converting Object



  

HTML - DOM Element

toString() Method

Converting Object to String

Supported Browsers

Method Chrome Edge Firefox Safari Opera
toString() Yes Yes Yes Yes Yes
html_dom_element_reference.htm
Advertisements