setTimeout is not a part of JavaScipt
Maybe you think setTimeout like this -: The setTimeout() method calls a function or evaluates an expression after a specified number of milliseconds. it's true, but let's dive deeper.
There aren't any methods like
Some web APIs:
- DOM APIs
- and others
setTimeout() is present inside
window object then why we don't write like this:
window.setTimeout(() => console.log('Timer finished'), 1000 );
Instead we write without
setTimeout(() => console.log('Timer finished'), 1000 );
window is a global object and
setTimeout() is present inside global object(at global scope), so we can access
window object. Not only with
setTimeout(), we don't need to write
window object to access
localstorage, and other web APIs.
Far by we've discussed that the browser provides web APIs, so we can use the things like
setTimeout() but nodejs is not a browser. Then how is it possible to access
setTimeout() in node. Well, we don't have
window object available while working with node. But we can still access
setTimeout(), let me tell you something at this point
setTimeout() function in node works similar to
window.setTimeout(), however they are not exactly same. Read more about this here.