I have not been able to find a proper explanation to this.
function subtract(x, y) {
setTimeout(function(x, y) {
document.write(x - y);
}, 1000);
}
subtract(1, 1);
This writes NaN to the document because the x and y parameters passed to setTimeout are undefined. You'd think that x and y should be integer 1 and it's within scope of the subtract function. The following snippet works perfectly fine and writes integer 0 as expected:
function subtract(x, y) {
setTimeout(function() {
document.write(x - y);
}, 1000);
}
subtract(1, 1);
Removing the parameters seem to solve the problem. Why?