DEV Community

Mitesh Kamat
Mitesh Kamat

Posted on

Third argument in setTimeout()

Introduction

This is about the third argument in setTimeout function.

As we know, setTimeout allows us to run a function once after the interval of time.
This is the general syntax,

let timerId = setTimeout(func|code, [delay], [arg1], [arg2], ...)
Enter fullscreen mode Exit fullscreen mode

In day to day usage, we use setTimeout() like this:

function greeting(){
 alert('hey!!')
}
setTimeout(greeting,1000);
Enter fullscreen mode Exit fullscreen mode

Let's see how we can pass the third argument

function greeting(arg1, arg2){
 console.log(arg1,arg2)
}
setTimeout(greeting,1000,"Hi", "There");
//output: Hi There
Enter fullscreen mode Exit fullscreen mode

Instead of string we can pass an array, object, etc.

function greeting(arr){
 console.log(arr);
}
const arr = [1,2,3,4]
setTimeout(greeting,1000,arr);
//output:  (4) [1, 2, 3, 4]


function greeting(person){
 console.log(person);
}
const person = {name:"abc", age: 21}
setTimeout(greeting,1000,person);
//output: {name: "abc", age: 21}
Enter fullscreen mode Exit fullscreen mode

That's how you can make use of the third argument.
Cheers !!!

Discussion (1)

Collapse
majedfaris profile image
majed L

great