92

If it's a scripting language as the name implies it must be written in a lower level language right? Like how PHP is written in C what language is JavaScript written in?

Sergio
  • 1,037
  • 1
  • 8
  • 5
  • 9
    "it must be written in a lower language", not necessarily, for example, [Narcissus](https://github.com/mozilla/narcissus/) is a Javascript interpreter, written in pure Javascript (it's a [meta-circular evaluator](http://en.wikipedia.org/wiki/Meta-circular_evaluator)) :) – Christian C. Salvadó Aug 10 '11 at 04:22
  • I am guessing there are a number of different JavaScript engines written in all sorts of different languages. – Galik Nov 29 '14 at 03:17
  • @CMS Yes, but the underlying question here is, what language first JS interpreter is written? You cannot write JavaScript Interpreter in JavaScript, when you have no tool to read you JavaScript code (Which is Interpreter in this case). This link here explains this chicken egg problem: https://stackoverflow.com/questions/18247888/how-can-a-c-compiler-be-written-in-c – RegarBoy Nov 12 '19 at 12:45

7 Answers7

140

Javascript is just a standard, more formally known as ECMAScript. It can be implemented in any language, just like any standard.

Chrome's Javascript engine, V8, is written in C++.

From the project page:

V8 is written in C++ and is used in Google Chrome, the open source browser from Google.

V8 implements ECMAScript as specified in ECMA-262, 5th edition, and runs on Windows (XP or newer), Mac OS X (10.5 or newer), and Linux systems that use IA-32, x64, or ARM processors.

Firefox's Javascript engine, SpiderMonkey (and now TraceMonkey) is also written in C++. And as maerics below said, Rhino is written in Java.

Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
  • 8
    Indeed. Although I'd wager that [the majority of them are implemented in C++](http://en.wikipedia.org/wiki/List_of_ECMAScript_engines) (or C and its derivatives). Rhino is obviously Java. – maerics Aug 10 '11 at 04:03
  • https://stackoverflow.com/questions/18247888/how-can-a-c-compiler-be-written-in-c – RegarBoy Nov 12 '19 at 12:42
  • Is Python a standard also? Is every language except C and C++ a standard? I can't find any information. on the differences. – Nakul Tiruviluamala Nov 09 '21 at 01:17
  • So is JavaScript just an intricate conceptual idea? E.g. in the same way I could write down the the specifications for a House (it must have 1 door, 2 rooms, 2 windows per room, running water, electricity etc), then other people could interpret those specifications, build a house and call it a House? I am assuming the answer is no, because there was a creator of the language and surely he actually made a functioning 'thing'. What was *that* initial *thing*? What did it look like? And what language was it written in (or was it 'just' an abstraction/shorthand for machine code)? – user1063287 Nov 13 '21 at 10:54
  • PS - maybe the first ‘thing’ created was the first JavaScript engine, SpiderMonkey, which was written in C: https://en.m.wikipedia.org/wiki/SpiderMonkey , see also: https://brendaneich.com/2011/06/new-javascript-engine-module-owner/ – user1063287 Nov 13 '21 at 15:14
40

All the answers so far are correct, but since it hasn't been mentioned yet, JavaScript can be written in JavaScript.

Matthew Crumley
  • 101,441
  • 24
  • 103
  • 129
12

Most Javascript interpreters are written in C/C++ (V8, Nitro, etc…), however a compliant interpreter can be written in any language (Rhino→Java, Interpreter→Javascript, etc…).

gf3
  • 481
  • 2
  • 11
8

Whichever language the client webbrowsers javascript interpreter was written in :)

Tom Studee
  • 10,316
  • 4
  • 38
  • 42
8

Javascript is an implementation of the ECMAScript standard, but there is no singular canonical interpreter like you see with PHP.

Most of the major implementations (standalone or as parts of web browsers) out there tend to be largely written in C or C++ for performance reasons, but that's not necessarily always the case. Rhino, an engine maintained by Mozilla, is written in Java.

dfreeman
  • 2,834
  • 2
  • 20
  • 24
0

Most implementations of Javascript show behaviour that is clearly caused by the use of pointers and byref parameter passing, which normally points towards the use of C, or C++

This is clearly notable for instance when you are taking apart a multidimensional array in a loop, with the help of intermediate array's. These tend to behave very "strangely", if you are not familiar with pointers and byref passing of parameters (You need do var hlp = new Array() each time or it will overwrite the previous values which you already stored somewhere else)

I'm rather curious as to how an implementation of javascript in for instance Java, because I imagine that this kind of behaviour will be quite different in that case?

  • 3
    No. The semantics of JavaScript execution do not depend on the language used to implement them. – Bergi Jul 26 '20 at 20:55
  • You can write functions under the hood to bypass that behavior (i.e. new and garbage collect in c++ instead of setting pointer). The fact that Javascript (technically EMCA) behaves the way it does has little if anything to do with its implementation language because it's just a standard – Alec Jul 15 '21 at 17:33
-5

C++ is the fundamental language for anything modern and fancy. Most modern high level languages are subset of low level language,C++. All modern languages you see today is a subset of C++ someway or the other. Even Java is a subset of C++.

  • 1
    This is wrong. Java is a not a subset of C++. C++ is not considered a low level language. You might be thinking of C, and even then Java and other languages are not a 'subset' of C. They might be *based* on C, but they are not a subset. The accepted answer gives a better answer, which is that Javascript can be writeen in any language, but that the most popular implementation is in C++ ie Google's V8. – David Frick Feb 26 '20 at 18:55
  • 2
    I do not understand what you mean by that. C++ is another language that originated as a superset of C. If any language is a subset of C++, then this python code should work in C++: `print("hello world")`. Paste it into a new file and compile it with a C++ compiler. Does it compile? If it doesn't, then Python is not a subset of c++ – ARI FISHER Nov 30 '20 at 16:09