DEV Community

Arhan Khan
Arhan Khan

Posted on

Build a Hangman Game with JavaScript

Hangman is a word-guessing game: a hidden word, a row of blanks, and a limited number of wrong guesses. It is a great beginner project because it teaches arrays, string handling, keyboard input, and game state in one small app.

What you are building

The app picks a random word and shows one blank per letter. The player guesses letters by clicking buttons or typing. Correct guesses reveal the letters; wrong guesses draw a part of the hangman. Six wrong guesses and the game is over.

The HTML structure

<p id="word-display"></p>
<p>Wrong guesses: <span id="wrong">0</span> / 6</p>
<p id="message"></p>
<div id="letters"></div>
<button id="restart">New Word</button>
Enter fullscreen mode Exit fullscreen mode

Generate the letter buttons from JavaScript instead of writing 26 buttons by hand.

The game logic

Keep the word, the guessed letters, and the wrong count in simple state:

const words = ["javascript", "computer", "keyboard", "program", "developer"];
let word = "";
let guessed = [];
let wrong = 0;
const maxWrong = 6;

function startGame() {
  word = words[Math.floor(Math.random() * words.length)];
  guessed = [];
  wrong = 0;
  render();
}

function render() {
  const display = word
    .split("")
    .map((letter) => (guessed.includes(letter) ? letter : "_"))
    .join(" ");
  document.getElementById("word-display").textContent = display;
  document.getElementById("wrong").textContent = wrong;
}

function guess(letter) {
  if (guessed.includes(letter) || wrong >= maxWrong) return;
  guessed.push(letter);

  if (!word.includes(letter)) {
    wrong++;
  }

  render();
  checkEnd();
}

function checkEnd() {
  const won = word.split("").every((letter) => guessed.includes(letter));
  const message = document.getElementById("message");
  if (won) message.textContent = "You win! The word was " + word;
  if (wrong >= maxWrong) message.textContent = "Game over! The word was " + word;
}
Enter fullscreen mode Exit fullscreen mode

Drawing the hangman

You can draw the hangman with a canvas, adding one part per wrong guess — the pole, the head, the body, the arms, and the legs. Or keep it simpler: show the wrong letters in red and a count. The canvas version is more fun, and each part is just a few lines:

const ctx = document.getElementById("hangman").getContext("2d");
// One line per wrong guess: pole, beam, rope, head, body, legs
const parts = [
  () => ctx.fillRect(10, 10, 5, 150), // pole
  () => ctx.fillRect(10, 10, 100, 5), // beam
  () => ctx.fillRect(105, 10, 5, 30), // rope
  () => { ctx.beginPath(); ctx.arc(107, 55, 15, 0, Math.PI * 2); ctx.stroke(); }, // head
  () => ctx.fillRect(105, 70, 4, 50), // body
  () => { ctx.beginPath(); ctx.moveTo(107, 120); ctx.lineTo(90, 150); ctx.moveTo(107, 120); ctx.lineTo(124, 150); ctx.stroke(); }, // legs
];
Enter fullscreen mode Exit fullscreen mode

What to add next

Add categories so the player picks a theme — animals, food, or programming words. Add a hint button that reveals one letter at the cost of a guess, or a score that tracks wins across rounds.

Top comments (0)