JavaScript createCaption() Method Tutorial

In this section, we will learn that the createCaption() method is and how to use it in JavaScript.

What is createCaption() Method in JavaScript?

The JavaScript createCaption() method is used to create a <caption> for a table.

You should know that if the target table already has a <caption> element, then using this method won’t add a new one, but instead returns a reference to the one that is already created.

Note: you can use the deleteCaption() method to remove a <caption> element from a table.

createCaption() Method Syntax:


createCaption() Method Parameter

The method does not take an argument.

createCaption() Method Return Value

If the target <table> did not have a <caption> then calling this method will create a new one and return a reference to that element.

But if the target table has a <caption> already, then the return value of this method will be a reference to that caption instead. This means in such case, the method won’t create a new one.

Example: using createCaption() Method in JavaScript

See the Pen using createCaption() Method in JavaScript by Omid Dehghan (@odchan1) on CodePen.

Leave a Reply