My question is setTimeout function and other web ApIs written in another programming language ? how javascript uses the other programming language features because other programming language syntax is different. Is there any translator between them which converts the code of another language in javascript ?.
2 Answers
There's no language translation involved, no, although there's definitely a boundary between the environments.
When a browser uses a JavaScript engine, the browser provides some things to the engine in order for the engine to do its work. One of those things is the global object, which has methods and properties on it that provide host-specific features (like the DOM, setTimeout, etc.). The browser also provides functions to the JavaScript engine that aren't exposed to JavaScript code, for doing things like resolving modules.
Think of the JavaScript engine as a library embedded in the browser application. The browser code calls into the library to do things like create a new environment for a window/tab, and provides functions to the library that it calls to do things like schedule timer callbacks.
Comments
Those functions exposed from the browser.
Basically it goes like
lex the js
do proper actions (wait for the timeout for instance)
push the callback back to js stack (possibly with extra data from the result of the native function)
about your second question whether there are any "transpilers" transpiling oter languages to js:
There are plenty of transpilers the simplest example is tsc which transpiles typescript to javascript
Also you didnt ask but I feel like you want to learn how things work so I will add a bonus.
WebAssembly is a binary format for the browser and it is a compiler target for many languages which means without bothering to transpile things to js you can simply comile your native program to webassembly and run native code in your browser
setTimeoutis not a part of JavaScript. That's implemented by browser's interpreters, say v8 Engine. JavaScript supports only sequential code execution. You might like to readevent-loopin JS as well.setTimeout, the host does. (V8 does have asetTimeoutplaceholder -- you can see this if you run V8 without any host -- but it doesn't actually do a timer, it just calls the function immediately.)