The Difference between JavaScript and Java

Many non-programmers and young programmers are confused by the names Java and JavaScript. In fact, JavaScript was renamed to JavaScript as an afterthought and a co-marketing campaign together with Sun (the creator of Java). But there are succinct jokes for this naming coincidence: Java and JavaScript are the same like car and carpet, or ham and hamster.

First, the similarities. On a cursory look, some of the syntax of Java and JavaScript code look alike. This is because they're somewhat descended from C (putting them in the Algol family but they are both more similar to C than Algol): nested code blocks within curly braces (except optionally for single statement blocks), nonsignificant whitespace (as opposed to Python), functions called with parens () with arguments inside (as opposed to Lisp), // and /* */ comments (as opposed to Bash shell scripting or Tcl insanity), C-like control keywords (if, else, while, for), special characters for mathematical or logical infix functions (=, +, *, &&, ==), the .-operator on objects, statements ending in ';' (optional in JavaScript)...

Both languages can run server-side. Neither language runs directly on a machine's CPU or hardware. Java code is compiled into an intermediary form called bytecode (as opposed to machine code) and then run on a virtual machine (JVM). JavaScript is an interpreted language meaning it isn't precompiled (however, optimizing JavaScript interpreters may precompile chunks of code, and there are also transpilers which convert variants of the JavaScript language down to a more standard one). There is an initiative to compile a subset of JavaScript called Asm.js into fast bytecode. But in any case (excepting for JIT) there is a buffer layer between code and CPU. In the past, this extra layer between execution would have meant slow performance but nowadays this problem has largely been addressed by the virtual machines / interpreters.

Both languages handle memory management for you with automatic garbage collection (as opposed to manual malloc/free in C). This doesn't mean there can't be memory leaks. But accidently overwriting memory shouldn't happen.

Now for some differences. Like its predecessors (C) Java is statically typed. Variables need to be declared with types, as do functions. Types can't be mixed with the same variables. Java is largely based around classes (but both languages allow object-oriented programming). JavaScript can be written with just functions.

JavaScript, on the other hand, allows variables to change type and allows for implicit type conversion and comparison. This means you don't need to write code which is as verbose as Java, but also that the (nonexistent) compiler can't catch certain classes of bugs for you. The TypeScript language was invented to make JavaScript into a strongly typed language with the ability to catch such type errors. TypeScript is one of these languages that transpiles down to standard JavaScript.

JavaScript's objects are Prototype-based, unlike most common object-oriented programming languages (including Java). EcmaScript 6 (EcmaScript being the official standards body name for JavaScript) has added some syntactic sugar with the class keyword. There are still differences between this and Java. JavaScript classes can only inherit from one class.

JavaScript is a functional language too. Java, less so. Java 8, the most recent release of Java, more so, but still not really. JavaScript supports anonymous functions, closures, first class functions, higher order functions, functions returning functions, etc.

There's an outdated misconception that JavaScript only runs in the browser. In fact, with Node.js, it runs on servers and other non-browser platforms just fine. There's also the conception that Java can run in any browser. In fact, Java requires plugins to run in browsers and these plugins have effectively been deprecated and thus browsers do not run Java.

So Java and JavaScript are two completely separate languages. While there's a way in new Java to embed JavaScript code and run it in Java, you generally cannot exchange code. A lot of people use Java because that's what they learned in school (now being replaced by Python). A lot of people use JavaScript because it's the only language choice they had for the browser. They are merely two among many useful modern programming languages.

Add new comment