Lesson 71 +15 XP

What is Asynchronous JavaScript?

What is Asynchronous JavaScript?

Asynchronous code lets the program keep running while waiting for something slow, like a network request or a timer.

The problem

Some tasks take time: loading data, waiting for a timer, downloading files. If the browser froze during each one, the page would be unusable.

Synchronous vs async

  • Synchronous: each task waits for the previous one.
  • Asynchronous: tasks can run in the background, and the rest of the code keeps going.

A timer example

console.log("Start");

setTimeout(function() {
  console.log("After 2 seconds");
}, 2000);

console.log("End");

Output: Start, End, then (2 seconds later) After 2 seconds. The code does not wait.

Common async operations

  • Timers with setTimeout and setInterval.
  • Fetching data from servers.
  • Reading files in Node.js.
  • Waiting for user input.

Why it matters

Async code is what lets apps load data without freezing, and it is the foundation of modern web development.

TL;DR

  • Async code runs in the background.
  • The rest of the program continues while waiting.
  • Timers and network requests are async.
  • It keeps web pages responsive.