Can we create HTML elements in a JavaScript file?

Question

Can we create HTML elements in a JavaScript file?

Answer

We absolutely can! In a JavaScript file, we can use DOM methods and properties to create (or insert, update, delete, etc) HTML elements stored as strings in a JS variable (or as a string passed to a DOM method/property).

However, when using the JavaScript library React, we will normally use JSX to create HTML elements inside a JavaScript file which uses different syntax than we would use if we were using DOM methods/properties.

13 Likes

Thank you for that explanation. It’s pretty clear now to me, however, and example of a DOM method would make it much more evident. Could you provide one?

Thank you.

what is stopping you to use google to find the relevant information? Learning to find and understand the information you are looking for is a good skill to have as a developer :slight_smile: