DEV Community

Arhan Khan
Arhan Khan

Posted on

Build a Movie Search App with JavaScript

A movie search app is the perfect second project after a to-do list. It teaches you the skill every real app needs: talking to an API, waiting for the answer, and showing the result. Here is a complete version you can build in an afternoon.

The plan

  1. An input where the user types a movie name.
  2. A fetch call to a movie API (OMDb has a free tier) when they search.
  3. A grid of cards showing the poster, title, and year.
  4. A friendly message when nothing is found.

The HTML

<input id="search" type="text" placeholder="Search a movie...">
<button id="btn">Search</button>
<div id="results"></div>
Enter fullscreen mode Exit fullscreen mode

The JavaScript

const API_KEY = 'YOUR_OMDB_KEY';

async function searchMovies(query) {
  const res = await fetch(
    `https://www.omdbapi.com/?apikey=${API_KEY}&s=${encodeURIComponent(query)}`
  );
  const data = await res.json();
  return data.Search || [];
}

function showMovies(movies) {
  const box = document.getElementById('results');
  if (movies.length === 0) {
    box.innerHTML = '<p>No movies found. Try another title.</p>';
    return;
  }
  box.innerHTML = movies.map(m => `
    <div class="card">
      <img src="${m.Poster !== 'N/A' ? m.Poster : 'placeholder.jpg'}" alt="${m.Title}">
      <h3>${m.Title}</h3>
      <p>${m.Year}</p>
    </div>
  `).join('');
}

document.getElementById('btn').addEventListener('click', async () => {
  const query = document.getElementById('search').value.trim();
  if (!query) return;
  const movies = await searchMovies(query);
  showMovies(movies);
});
Enter fullscreen mode Exit fullscreen mode

Three details beginners miss

  • Always encodeURIComponent the query. A movie like "Fast & Furious" breaks your URL without it.
  • Handle the N/A poster. Some movies have no poster, and a broken image icon looks worse than a placeholder.
  • Show a loading state. Set the results area to "Searching..." before the fetch, so the user knows the app heard them.

What to add next

Click a card to fetch the full plot and rating with a second API call, save favourites to localStorage, or add a debounce so the search runs as the user types. Each of those is one small feature — and each one teaches you something a tutorial alone never will.

Top comments (0)