It is sometimes necessary to pass a variable to a SetTimeout() callback function. By the time the callback occurs, the original variable may no longer exist, so passing a copy of it as a third parameter in SetTimeout() solves this issue.

In this case, we want the callback function to output the value of name as it was at the time the callback was triggered (“My Original Name”), and not the value at the time the callback occurred (“Some New Name”).

var name = "My Original Name";
setTimeout(function(x) {
}, 1000, name);
name = "Some New Name";

// Output: My Original Name

If we didn’t pass the value in as a variable, the callback would reference the value of name at the time of the callback, by which point it has been changed to “Some New Name”.

More information: