If there was an award for the most confusing branding name, "JavaScript" would surely be a nominee. It has "Java" in the name - so you'd think it'd be related to Java in some way, maybe JavaScript is part of Java or something.

That's how it usually works, right?

A Ferrari SF90 is a type car made by Ferrari. An Apple Macintosh is a type of computer made by Apple. So JavaScript must be a programming language made by the same folk who develop Java. An off-shoot of some sort, perhaps.

Except it isn't! The two languages might share the name Java, but they are definitely not the same.

So here's everything you need to know about these two languages, and the main differences between them.

A brief history of JavaScript

(If you’d rather skip this brief history lesson just get straight into the differences between Java and JavaScript)

In the early days of the web, pages were totally static - no dynamic interaction happened once the page had loaded. Netscape, one of the early browser pioneers, wanted to change all that.

First, they tried embedding Java into their browser. Java was the hot programming language at the time (around the mid-1990s), and it was designed to be used pretty much anywhere (more on that later) - so this made sense.

However, Netscape soon decided that Java wasn't the best option for dynamic websites. Instead, they decided to make their own language. So they got a guy named Brendan Eich to make the new language, which he did in about 10 days (Eich also co-founded Mozilla, created the Brave browser, and developed the BAT cryptocurrency!).

Is JavaScript based on Java?

Eich based the syntax of the new language on Java, which is why the two languages look pretty similar in a text editor. And it makes heavy use of objects. So there was some influence there, perhaps, but it's definitely not an off-shoot, fork, or a version of Java. It's its own thing entirely, and works quite differently under the hood.

So why did they put "Java" in the name?

That might have been a tip of the hat to Java's influence on JavaScript's syntax. Actually, JS was first called "Mocha", but the name was changed to "LiveScript", and later changed again to "JavaScript" (I guess "Mocha" was too subtle). On the other hand, many say the name JavaScript was chosen purely as a marketing ploy - to hitch a ride on the Java success train, as it were. Whether that's true or not, it's the name we're stuck with!

OK, history lesson over - now let's compare the two. What do Java and JavaScript have in common? What are the main differences?

Main differences between Java and Javascript

  • Java needs to be compiled. JavaScript doesn't, it's interpreted.
  • Java uses static typing. Javascript uses dynamic typing.
  • Java is a class-based language. JavaScript is a prototype based-language.
  • Java is an object-oriented language. JavaScript is a object-based scripting language.
  • Java files use the ".Java" extension. JavaScript files use the ".js" extension.
  • Java is a strongly typed language. JavaScript is a weakly typed language.

Java is compiled

Java was designed with a Write Once Run Anywhere (WORA) philosophy in mind. You probably know that the Android OS is written in Java, as are native Android apps. You may also know it can be used to create games, run web servers, and build desktop apps. But did you know it's also used to create UIs for Blu-ray/DVD players, TVs, and car dashboards? Java runs everywhere from parking meters and vending machines to dishwashers and ATMs.

But how can it run in all these different systems? The answer is the Java Virtual Machine, or JVM - the thing that runs Java code. See, when you type Java code, you write it in human readable syntax, maybe like this (note how it looks similar to JS):

private static void b() {
int n = -1;
if (k.q) {
n = 1;
}
}

You put this code in .java files, but the JVM can't read these. So you compile these .java files into .class files. They become bytecode, which is completely unreadable by humans. In fact, if you try to open a .class file in a text editor, you'll probably get either a garbled mess of characters, an error message, or both. If you open a .class file in a hex editor, you'll see something like this:

What a class file looks like in a hex editor

You and I can't read that, but a JVM understands it perfectly. So to get a Java program running on a dishwasher, all you need is some hardware with a JVM on it, and a compiled Java program that is able to interact with that hardware.

xkcd comic Golden Hammer

JavaScript is interpreted

JavaScript doesn't work that way - it's not compiled, it's interpreted. So the .js file you write your JavaScript code into is the exact same one that the browser reads.

This is why you can get weird behaviour when running faulty JS code - the interpreter will run the code from the beginning, and any parts with no errors will run, but it will start complaining once it gets to an error. In Java, the whole code would be parsed when compiling, and it would refuse to compile if it finds an error.

This means JavaScript doesn't have that WORA philosophy - but it makes it easier to get started with. All you need is a text editor (which you already have) and a browser (which you already have), and you can start writing code.

With Java you'll need to download a compiler too, and an environment in which to run your code. And when you make a change, you'll have to recompile everything to see how your changes look and work (which can sometimes take a loooooong time).

Relevant xkcd showing two coders sword fighting while waiting for their code to compile.

Are Java and JavaScript object-oriented?

With object-oriented programming, you build your code around classes and objects. An object represents a 'thing' (a DB connection, a bad guy in a game, an element on a website), and it contains the data (fields), and behaviour (methods) of that 'thing'.

A class is a blueprint for an object. So you maybe have a class "Badguy", which would give the basic details of a badguy. Its data would be things like height, width, health, speed. Its behaviour would be its movement, attacks, collision detection, and so on. Every bad guy on the screen would be an object of that class and would inherit the default data and methods from that class.

Both Java and JavaScript use objects, but only Java uses classes (with Java, you must have at least one class). Most people won't get upset if you say Java is object-oriented - but it's possible to make the case that it isn't - at least not in a pure, technical sense.

Whether JS is object-oriented is a bit more up for debate.

With JavaScript, almost everything is an object. When you use document.getElementById(), you are using the getElementById() method of the document object. Anything that method returns will be an element object, which inherits its default properties from the base element object. So while JS doesn't technically have classes, it kinda acts as it does. In fact, in JavaScript, even primitive data types can be used in object-like ways: "Hello".length will return 5, for instance.

So is JavaScript object-oriented? Well, people will argue both sides of that - but you can certainly write your code as if it is. But you can also write it in a non-object-oriented, more procedural type of way (even if behind the scenes, everything you're writing is contained within an object).

This gives you more flexibility - with Java, you have to do things in a class-based, object-oriented way. The advantage here is that you're planning better for the future - it will be easier to adapt your code for other situations. The disadvantage is, if you know that you won't need to extend or reuse this bit of code, you may end up over-engineering and wasting time.

A relevant xkcd about object-oriented programming.

Java vs JavaScript: which is better for the web?

Back in the day, Java was used on websites using Java applets - basically, little Java programs that ran in a JVM on the computer, and were inserted into the browser, kind of like an iframe. But those days are gone, and JavaScript has absolutely won the web race. If you're interested in developing websites or web apps, JS is the only game in town.

In fact, JavaScript is being used more and more for mobile app development. So to make a native app for Android or iOS, you'd normally need to learn Java or Objective C, respectively. But JS frameworks like React Native now mean you can take the skills you use when making web apps, and use them to build mobile apps, too.

Using plugins and frameworks to improve your projects

One thing that Java and JavaScript have in common is extensibility. With Java there are tons of modules and plugins that you can import into your apps. And with JavaScript it's very much the same - there is a huge community of developers who write and test code that you can use in your projects.

The idea here is simple - why reinvent the wheel? If someone has made a great framework that does what you want to do, and it's widely used and tested, why not use it?

For example, take fullPage.js. This is a JS framework that helps you make glorious full page sites. You know the sort - the users scroll down, and they are taken smoothly down to another full page screen. These are perfect for your home page, or for pitch pages for your key products.

If you wanted to create a site like that, it would take a long time to get running and tested (especially if you wanted to use cool transitions between the pages, like this drop effect) - so why not use a solution that works out of the box? Check it out!

About the author:

Warren is a front end developer based in the UK.
You can find more from him at https://warrendavies.net