CodeNewbie Community 🌱

Candice Tullis (She/Her)
Candice Tullis (She/Her)

Posted on

Is JavaScript the only programming language that works in the browser?

Is JavaScript the only programming language that works in the browser? I suppose I'm wondering because from what I understand, it's the only language that's embedded in all major browsers.

If it's NOT the only one, why does it basically have a browser monopoly?

Thx in advance! 🎉

Top comments (5)

Collapse
 
djuber profile image
Daniel Uber • Edited

The short answer is probably yes. If you want to program the browser and add interactivity or interesting behavior to the page, javascript is the best way to do that.

The long answer is "it's complicated" (isn't it always?) - and almost all the complication stems from the idea that while the browser only knows how to execute javascript, people don't have to write it. This is not really different from the idea that a computer only knows how to execute binary machine files, and the same techniques to work around that apply. We write code in higher level languages, and we use a program to generate the machine file. The only difference is now there are programs to generate javascript instead of machine code.

Transpilers free humans from writing javascript

There are a number of languages that compile code to javascript (instead of to machine code), where you write the code you mean to, in a language that you prefer or that you understand better, run a few commands to tell the system to turn your code into javascript, and get a javascript .js file as output instead of .exe files or other machine executable programs. Coffeescript is an early example of this, but these days, even modern javascript is normally compiled to "legacy" javascript to ensure browser compatibility. Typescript would be a great example of both a language that compiles to javascript, and a javascript that compiles to javascript.

You might also find server-side frameworks that abstract the javascript generation away for you, you add some declarations to the code to indicate there are "active" parts in the display, and the right glue is added to the html, and needed js is spliced in, and the front end still runs javascript, but you never saw or wrote it.

Network Effects Drive the Single Language as a Standard

Browser vendors have spent a lot of time and effort into making javascript engines run as efficiently and quickly as possible, which made javascript a more attractive target than if it were slow or limited, and this caused a reinforcing cycle where more front end javascript code on websites, expecting more performance, drove more pressure to optimize the web browsers "engines" to support javascript. Adding a new scripting language into browsers would hit a few problems, the first being that only one or two specific browsers would support it (this happened with another extension framework used by Microsoft's Internet Explorer called ActiveX) - and a developer that wanted to make their application as usable as possible to a wide audience would need to use Javascript, or to write the program twice, once in the new extension language that only works on their chosen browser, and again in javascript to support everyone else. Secondly, getting really high performance out of a new language (so that the new language seemed better than the current javascript engines) is hard, and keeping a performance advantage over a technology with large ongoing investment from companies like Google and Apple will be a full time job. Thirdly, if it's just your language (the syntax of the code is easier to read and write), and not the specific browser implementation, that's exciting or powerful, someone will probably write a translation layer to output javascript for it, so the code can be used in other browsers. At this point, why would you continue all the hard work to maintain a second extension language?

Other ways to program for the web

There was a general purpose subset of javascript that behaved like the output of a traditional computer program, called Asm.js, that was used to permit any language that normally compiles programs to machine language to compile to javascript instead, the parts of the javascript language that were selected were special and "low level", and chosen so that they were the fastest to execute in the browser. There's also a subsequent effort to add a language independent runtime to browsers, called WebAssembly, which is a target platform for other programming languages (C, C++, Go, and Rust are good examples of languages that target webassembly already), but I believe you still need to add glue code in javascript on the page to get the browser to load and run the webassembly code on a page. There are still some limits on the things you can do in webassembly - it's not designed to replace the things javascript is designed specifically to be good at, like adding interactivity to the elements of webpages, but it's a good way to push more computation/calculation into the browser, which you call from the page via javascript (a little like a library). WebAssembly might be the exception to my earlier point about adding a second extension language, and it has substantial participation from the browser makers, so you probably can use it in modern browsers already (and gracefully fall back to asm.js if it's not available). There might be a world in the future where browsers let you run the full program just by loading the webassembly file (not an html page that includes javascript that loads and calls webassembly that contains the program), I don't think that's happened yet.

Good to Know Javascript

Whether you find you need to write a lot of javascript yourself, you will probably read a lot of it, mostly written by other people, so familiarity with the language will help you no matter what. I can't think of a job or web based skill that's not helped by knowing javascript. Most of the languages that output javascript tend to either expect you know the names of functions in javascript, or the behavior of the underlying javascript functionality, while they might change the capitalization of getElementById to get_element_by_id you probably will have a function with the same, or almost the same name, that does this exact thing, and the documentation for javascript will be easier to find than for your specialty language for a long time still, and many of the specialty language authors might just point you to the javascript documentation at some point.

Collapse
 
foxnotmegan profile image
fox-not-megan • Edited

The thing is that Javascript programming is one of the most developed niches for today and high-level of appliancy of the language makes it the most in demand on the market and reduces the need of developing any other coding languages as well as spreading those in tech community.

Collapse
 
ender_minyard profile image
ender minyard

I would disagree with Daniel's answer and say JavaScript is not the best way to "program the browser".

Daniel's answer is quite interesting but WebAssembly is more capable than they seem to understand - you can add interactivity to a webpage with WebAssembly.

I personally prototyped an interactive webpage with JavaScript and then rewrote that interactive webpage in WebAssembly and the WebAssembly version was much faster. (JavaScript version was 1fps, WebAssembly version was 60fps.)

Collapse
 
anastasiiastefanuk profile image
Anastasiia Stefanuk

No, JavaScript is not the only programming language that works in the browser, but it is the primary language supported by all modern web browsers. However, there are other languages and technologies that can be transpiled or compiled into JavaScript to run in the browser. Some notable examples: TypeScript, CoffeeScript, Dart and
also WebAssembly.

Collapse
 
chrisjo93 profile image
Chris Johnson

Nope, just about all languages can run on the browser with a little fiddling. As Daniel was saying languages typically get transpiled into JavaScript anyway.