Slightly shorter code using insertRow and insertCell:

function tableCreate() {
  const body = document.body,
        tbl = document.createElement('table');
  tbl.style.width = '100px';
  tbl.style.border = '1px solid black';

  for (let i = 0; i < 3; i++) {
    const tr = tbl.insertRow();
    for (let j = 0; j < 2; j++) {
      if (i === 2 && j === 1) {
        break;
      } else {
        const td = tr.insertCell();
        td.appendChild(document.createTextNode(`Cell I{j}`));
        td.style.border = '1px solid black';
        if (i === 1 && j === 1) {
          td.setAttribute('rowSpan', '2');
        }
      }
    }
  }
  body.appendChild(tbl);
}

tableCreate();

Also, this doesn't use some "bad practices", such as setting a border attribute instead of using CSS, and it accesses the body through document.body instead of document.getElementsByTagName('body')[0];

Answer from Cerbrus on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ jsref โ€บ dom_obj_table.asp
HTML DOM Table Object
The Table object also supports the standard properties and events. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com ยท If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com ยท HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
๐ŸŒ
W3Schools
w3schools.com โ€บ html โ€บ html_tables.asp
HTML Tables
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP, Bootstrap, Java, XML and more.
๐ŸŒ
Valentino G.
valentinog.com โ€บ blog โ€บ html-table
Back To The Basics: How To Generate a Table With JavaScript
February 2, 2020 - Then there's the tbody (table body) containing a bunch of tr (table rows). Each table row contains a certain number of td elements (table cells). With these requirements in place we can start coding our JavaScript file.
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ API โ€บ Document_Object_Model โ€บ Building_and_updating_the_DOM_tree
Building and updating the DOM tree - Web APIs | MDN
October 20, 2025 - In the <table> tree of Figure 1 the element <table> has one child: the element <tbody>. <tbody> has two children. Each <tbody>'s child (<tr>) has two children (<td>). Finally, each <td> has one child: a text node. In this example we change the background color of a paragraph when a button is clicked.
๐ŸŒ
DataTables
datatables.net โ€บ examples โ€บ data_sources โ€บ js_array.html
DataTables example - JavaScript sourced data
A table must be available on the page for DataTables to use. This examples shows an empty table element being initialising as a DataTable with a set of data from a JavaScript array.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ how-to-create-and-style-tables-with-vanilla-javascript
How to Create and Style Tables with Vanilla JavaScript
October 27, 2025 - This is where our table finally appears on the page. We select the empty <div> (our placeholder) and append the built table to it. JavaScript does not automatically add tables. It only creates them because we told it to.
๐ŸŒ
Tabulator
tabulator.info
Tabulator | JavaScript Tables & Data Grids
var table = new Tabulator("#example-table", { data:tabledata, //load row data from array layout:"fitColumns", //fit columns to width of table responsiveLayout:"hide", //hide columns that don't fit on the table addRowPos:"top", //when adding a new row, add it to the top of the table history:true, //allow undo and redo actions on the table pagination:"local", //paginate the data paginationSize:7, //allow 7 rows per page of data paginationCounter:"rows", //display count of paginated rows in footer movableColumns:true, //allow column order to be changed initialSort:[ //set the initial sort order o
Find elsewhere
๐ŸŒ
DataTables
datatables.net
DataTables | Javascript table library
This table makes use of Responsive and ColumnControl. ... DataTables is a Javascript HTML table enhancing library.
๐ŸŒ
Team Treehouse
teamtreehouse.com โ€บ community โ€บ having-javascript-inside-a-table
Having JavaScript inside a table? (Example) | Treehouse Community
October 8, 2020 - Note: When using a table, the table should contain data. This means there are few circumstances where you would want to insert a <p> element into it. In this case, we're work with integers - so you can just insert them as that. ... <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>JavaScript Loops</title> <link href="style.css" rel="stylesheet"> <script src="while.js"></script> </head> <body> <main> <h1>Random Numbers Between 1 and 10</h1> <div id="cssLayout"> <h3>CSS Layout</h3> </div> <div id="tableLayout"> <h3>Table Format</h3> <table id="randTable"> <tr> <th>Count</th> <th>Rand Num</th> </tr> </table> </div> </main> </body> </html>
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ API โ€บ Document_Object_Model โ€บ Traversing_an_HTML_table_with_JavaScript_and_DOM_Interfaces
Traversing an HTML table with JavaScript and DOM Interfaces
To create a node call document.createElement("tagname"). For example: ... Nodes can be removed. The following code removes text node myTextNode (containing the word "world") from the second <p> element, secondParagraph. ... Text node myTextNode (containing the word "world") still exists. The following code attaches myTextNode to the recently created <p> element, myNewPTagNode. ... The following figure shows the table object tree structure for the table created in the sample.
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ HTML โ€บ Reference โ€บ Elements โ€บ table
<table>: The Table element - HTML - MDN Web Docs - Mozilla
H43: Using id and headers attributes to associate data cells with header cells in data tables | Techniques for W3C WCAG 2.0 ยท The examples below include tables of progressively increasing complexity.
๐ŸŒ
CloudDefense.ai
clouddefense.ai โ€บ code โ€บ javascript โ€บ example โ€บ table
Top 10 Examples of <!-- -->table<!-- --> code in Javascript | CloudDefense.AI
const sortedChanges = sortBy(changes, [ change =&gt; change.code, change =&gt; change.description ]); const breakingChanges = sortedChanges.filter( change =&gt; change.severity === ChangeSeverity.FAILURE ); sortBy(breakingChanges, change =&gt; change.severity); const nonBreakingChanges = sortedChanges.filter( change =&gt; change.severity !== ChangeSeverity.FAILURE ); result += table([ ["Change", "Code", "Description"], ...[ ...breakingChanges.map(formatChange).map(Object.values), // Add an empty line between, but only if there are both breaking changes and non-breaking changes.
๐ŸŒ
Gridjs
gridjs.io
Grid.js - Advanced JavaScript table plugin
Grid.js consists of two main files, ... nicely. ... new Grid({ columns: ['Name', 'Email'], data: [ ['John', '[email protected]'], ['Mike', '[email protected]'] ] }).render(document.getElementById('table'));...
๐ŸŒ
Listjs
listjs.com โ€บ examples โ€บ table
Example of how to use a table with List.js - List.js
Perfect library for adding search, sort, filters and flexibility to tables, lists and various HTML elements. Built to be invisible and work on existing HTML.
๐ŸŒ
Programiz
programiz.com โ€บ javascript โ€บ examples โ€บ multiplication-table
JavaScript Program to Display the Multiplication Table (with Examples)
To understand this example, you should have the knowledge of the following JavaScript programming topics: ... // program to generate a multiplication table // take input from the user const number = parseInt(prompt('Enter an integer: ')); //creating a multiplication table for(let i = 1; i <= ...
๐ŸŒ
CodeDrome
codedrome.com โ€บ creating-html-tables-with-javascript
Creating HTML Tables with JavaScript - CodeDromeCodeDrome
February 24, 2024 - The overall structure is pretty much the same here, but this function uses createElement and createTextNode for the table, caption, rows and cells, which are then added to their respective parents with appendChild. The advantage is that we are using "official" front-door methods to manipulate the DOM which is more robust. If we call, for example, document.createElement("tr") then we know that we will get a <tr> without worrying about typos or having to remember to add </tr> at the end.
๐ŸŒ
Toni-heittola
toni-heittola.github.io โ€บ js-datatable
JS-datatable examples
<table class="table datatable" data-id-field="code" data-sort-name="value1" data-sort-order="desc" data-show-chart="false" data-pagination="false" data-show-pagination-switch="false"> <thead> <tr> <th data-field="code" data-sortable="true">Name</th> <th data-field="value1" data-sortable="true">Value 1</th> <th data-field="value2" data-sortable="true">Value 2</th> </tr> </thead> <tbody> <tr> <td>item 1</td> <td>40.4</td> <td>6.5344</td> </tr> </tbody> </table> More detailed examples see examples below. In case you need customization. In case an extensive customization is required, the datatable can be created with javascript.