Course contentsShow
← JavaScript
Lesson 1204 of 2,72327. The Event LoopFree lesson

The setTimeout Function Signature

Learn the syntax of setTimeout, its parameters (callback, delay, additional arguments), and return value.

The setTimeout Function Signature

You'll learn the exact syntax of setTimeout, what each parameter does, and what the function returns.

What It Does

setTimeout is a built-in JavaScript function that schedules a callback to run after a specified delay. Understanding its signature means knowing what goes in and what comes out.

The Signature

const timerId = setTimeout(callback, delay, arg1, arg2, ...);

Let's break down each part:

Parameter 1: callback

The function you want to run after the delay. This can be a function reference, an arrow function, or even an anonymous function.

setTimeout(() => {
  console.log("Hello!");
}, 1000);

Parameter 2: delay

The minimum wait time in milliseconds before the callback runs. 1000 means one second. If you omit this or pass 0, the callback is scheduled "as soon as possible" (but still asynchronously).

Parameters 3+: arg1, arg2, ...

Optional arguments that get passed to your callback when it finally runs. Think of this as a way to send data into the future.

setTimeout((name, age) => {
  console.log(`${name} is ${age} years old`);
}, 1000, "Alice", 30);
// After 1 second: "Alice is 30 years old"

Return Value: timerId

setTimeout returns a numeric ID (a timer identifier). You can use this ID later to cancel the timer if needed (though we haven't covered cancellation yet).

const id = setTimeout(() => console.log("Hi"), 2000);
console.log(id); // A number like 1, 2, 127, etc.

Key Takeaway: setTimeout(callback, delay, ...args) schedules a callback to run after a delay, optionally passing arguments, and returns a timer ID for reference.