DEV Community

Arhan Khan
Arhan Khan

Posted on

Build a Dictionary App with JavaScript

A dictionary app is the best first API project, because the API is free, needs no key, and returns exactly the data you want to display. You type a word, and you get its meaning, its pronunciation, and an example sentence.

What you are building

An app that takes a word, calls the free Dictionary API, and shows the first definition, the part of speech, and an example — with a clear message when the word is not found.

The JavaScript: fetch and display

async function lookup(word) {
  const status = document.querySelector('#status');
  const result = document.querySelector('#result');
  status.textContent = 'Looking up...';
  result.innerHTML = '';

  try {
    const res = await fetch('https://api.dictionaryapi.dev/api/v2/entries/en/' + word);
    if (!res.ok) throw new Error('Not found');
    const data = await res.json();
    const entry = data[0];
    const meaning = entry.meanings[0];
    const def = meaning.definitions[0];

    result.innerHTML =
      '<h2>' + entry.word + ' (' + meaning.partOfSpeech + ')</h2>' +
      '<p>' + def.definition + '</p>' +
      (def.example ? '<p><em>"' + def.example + '"</em></p>' : '');
    status.textContent = '';
  } catch (err) {
    status.textContent = 'No definition found for "' + word + '". Check the spelling and try again.';
  }
}

document.querySelector('#search-btn').addEventListener('click', () => {
  const word = document.querySelector('#word-input').value.trim();
  if (word) lookup(word);
});
Enter fullscreen mode Exit fullscreen mode

Why this API is beginner-friendly

The Dictionary API at dictionaryapi.dev needs no signup and no key. That means your app works the moment you write the fetch() call — there is no dashboard, no token, and no setup to forget.

Handle the unhappy paths

Two things can go wrong: the word does not exist, and the network fails. Both land in the same catch block here, and both show the same honest message. A loading message matters too — without it, users click the button twice because nothing seems to happen.

The one thing to remember

Check res.ok before calling res.json(). A 404 response is still a successful fetch() — it only becomes an error when you check the status. Skipping that check is the most common fetch bug, and it produces confusing errors instead of a clean "not found" message.

Top comments (0)