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