In console it appears as native function but I'd like to know how they were constructed. For example what is the code which executes when pressing space bar to scroll the page. That info would teach me a lot, and I could make my functions more effective
-
2The code that scrolls the page won't be JavaScript, it'll be C++ or whatever language the browser was implemented in. But there are some open source browsers, so you can download the source code. – nnnnnn Mar 13 '14 at 06:57
-
duplicate? http://stackoverflow.com/questions/22370735/emulate-spacebar-scroll-function/22370829?noredirect=1#comment34005844_22370829 – Jorg Mar 13 '14 at 07:01
-
Ha ha, that is my question. But I asked not only the spacebar scroll function, but all the javascript coded – jscripter Mar 13 '14 at 07:39
-
See also [How to see the source of Built-in javascript functions?](https://stackoverflow.com/q/22300206/1048572) – Bergi May 23 '17 at 10:15
-
See also [Read JavaScript native code](https://stackoverflow.com/q/9103336/1048572) – Bergi Jul 26 '20 at 20:49
-
Does this answer your question? [How to see the source of Built-in javascript functions?](https://stackoverflow.com/questions/22300206/how-to-see-the-source-of-built-in-javascript-functions) – Nico Haase Jul 30 '20 at 12:59
-
There are different Javascript engines implemented. Sharing the top couple of ones. The one from chrome is called V8 and it's available here > https://chromium.googlesource.com/v8/v8.git The one from Mozilla is called SpiderMonkey and it's available here > https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey – Vasanth Gopal Jul 30 '20 at 12:52
4 Answers
- Pick a browser
- Make sure it is an open source one
- Dig through its source code
Some repositories include:
Note that JavaScript native functions are generally not written in JavaScript (expect C or C++ most of the time). They are just exposed to JS through an API.
Also note that the code that scrolls a page when the spacebar is pressed isn't even a function that is exposed to JS.
-
1so the reason when I log `toString`, the output is `ƒ toString() { [native code] }` and because those native functions, which were mostly written in C++ or Torque(some in v8), it compiles to machine code, so rather than displaying that machine code, the output is `[native code]`? – Caleb Taylor Jun 28 '19 at 20:08
While this will not show you actual source code, if you're interested in how many of the native JavaScript functions are implemented, you can peruse the specification upon which they are based:

- 3,998
- 24
- 33
What is V8?
- V8 is Google's open source JavaScript engine.
- V8 implements ECMAScript as specified in ECMA-262.
- V8 is written in C++ and is used in Google Chrome, the open source browser from Google.
- V8 can run standalone, or can be embedded into any C++ application.
- V8 project page: https://github.com/v8/v8/wiki
- V8 source code: https://github.com/v8/v8

- 8,656
- 7
- 45
- 58
The contract for exactly how the Javascript built-ins should behave is outlined in the ECMAScript specification (see example for Array.every()
).
There are a number of different Javascript engines, each with their own specific implementation of ECMAScript. The most common Javascript engines are (links point to code for Array.every()
):

- 10,764
- 2
- 38
- 66