Waiting 5 seconds in JavaScript is a common timer task for retries, delayed messages, and small pauses between actions. The usual tool for that job is setTimeout().
You can also wrap the timer in a Promise when you want a cleaner async flow. That becomes useful when you need to wait for async work to finish before continuing.
Tested on: Node.js v20.18.2. A short note after each runnable snippet describes what you should see in the console.
Method 1: Wait 5 seconds with setTimeout()
setTimeout() runs a callback after the delay expires. It does not pause the whole program.
setTimeout(() => {
console.log("wait-5-seconds: done");
}, 5000);You should see one line logging wait-5-seconds: done.
This is the simplest way to delay a one-time action.
Method 2: Build a reusable delay helper
A Promise wrapper lets you await the delay instead of nesting callbacks.
function wait(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function demo() {
await wait(10);
console.log("delay-helper: finished");
}
demo();You should see one line logging delay-helper: finished.
Use this pattern when you need a reusable wait function for retry loops or async steps.
Method 3: Delay work without blocking the rest of the code
The rest of the script keeps executing because timers are asynchronous.
console.log("start");
setTimeout(() => console.log("wait-5-seconds: later"), 10);
console.log("end");You should see 3 lines, in order: start, end, wait-5-seconds: later.
That non-blocking behavior is the reason setTimeout() is used so often in browser code and Node.js scripts.
Summary
To wait 5 seconds in JavaScript, use setTimeout() for a simple delayed callback or wrap it in a Promise when you need a reusable async helper. The important detail is that timers do not block the event loop, so the rest of the script continues to run while the delay counts down. That makes this pattern useful for retries, staged UI updates, and timed automation in both browsers and Node.js.
