Tic-Tac-Toe in JavaScript
Build Tic-Tac-Toe in JavaScript with nine cells and eight winning lines checked by some and every. Stop after a win or draw; two players share one browser.
Save this as index.html and open it in a browser.
1<!doctype html>2<html lang="en">3<meta charset="utf-8">4<title>JavaScript practice</title>5<body>6<h1>Tic-Tac-Toe</h1><div id="board" style="display:grid;grid-template-columns:repeat(3,60px);gap:4px"></div><p id="status"></p><button id="restart">Restart</button>7<script>8let board, player, finished;9const lines = [[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]];10const grid = document.querySelector('#board');11const status = document.querySelector('#status');12function render() {13 grid.replaceChildren();14 board.forEach((mark, index) => {15 const button = document.createElement('button');16 button.textContent = mark || '·'; button.style.height = '60px';17 button.setAttribute('aria-label', 'Cell ' + (index + 1) + ': ' + (mark || 'empty'));18 button.disabled = finished || Boolean(mark);19 button.onclick = () => move(index); grid.append(button);20 });21}22function move(index) {23 if (finished || board[index]) return;24 board[index] = player;25 const won = lines.some(line => line.every(cell => board[cell] === player));26 if (won) { finished = true; status.textContent = player + ' wins!'; }27 else if (board.every(Boolean)) { finished = true; status.textContent = 'Draw!'; }28 else { player = player === 'X' ? 'O' : 'X'; status.textContent = player + ' to move'; }29 render();30}31function restart() { board = Array(9).fill(null); player = 'X'; finished = false; status.textContent = 'X to move'; render(); }32document.querySelector('#restart').onclick = restart;33restart();34</script>35</body>36</html>
Sample Output
X wins!
Cells 1, 2, and 3 contain X after the five example moves. This matches the top-row winning pattern.
In this one
- Model nine board cells
- Test all winning lines
- Distinguish a win from a draw
- Block moves after the game ends
How it works
- 1
Create the board and eight winning index patterns.
- 2
Render one labelled button for each cell.
- 3
Place the current mark only in an empty active cell.
- 4
Check for a win, then a full-board draw, otherwise switch player.
- 5
Follow cells 1, 4, 2, 5, 3 to see X win across the top row.
Edge cases
Occupied cell
A player must not replace an earlier mark.
Buttons are disabled when occupied, and move also checks the board before writing.
Winning last move
A full board can still contain a new winning line.
Check the win condition first. Only call the result a draw when no line wins.
What's going on
Tic-Tac-Toe is a two-player game on a three-by-three board. Players alternate X and O, and the first to fill a row, column, or diagonal wins. Building it teaches how a page changes from an active game to a finished state.
The board is an array of nine values. The lines array lists all eight winning patterns by zero-based cell index. After a valid move, some checks whether at least one line has every cell equal to the current player's mark. That makes the win rule explicit instead of leaving checkWinner empty.
A win is checked before a draw, because the final available cell can complete a winning line. If neither condition ends the game, the player switches. Rendering disables occupied cells and all cells after the game finishes. Restart creates a new board and returns the turn to X. This local game is for two people sharing one browser; it has no computer opponent or online room.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Which condition should be checked first?
Pick an answer to check it.
Question 2
What does every check inside a line?
Pick an answer to check it.
Questions people ask
Why are there eight lines?
There are three rows, three columns, and two diagonals that can win.
Why keep a finished flag?
It stops extra moves after a win or draw, even if move is called from somewhere else.
Does X always begin?
In this version, yes. Restart sets player back to X.