Java and JavaScript are two different programming languages that happen to share part of a name. Java is statically typed and compiled to bytecode that runs on the Java Virtual Machine, while JavaScript is dynamically typed and is the language that web browsers run.
The names caused confusion from the start. MDN explains that JavaScript follows most of Java's expression syntax, naming conventions and basic control-flow constructs, which was the reason it was renamed from LiveScript to JavaScript. Syntax resemblance is where the family relationship ends.
Java vs JavaScript at a glance
| Java | JavaScript | |
|---|---|---|
| Typing | Static and strong: variable types are declared | Dynamic and weak: variables hold any type, mismatched types are converted implicitly |
| Object model | Class-based | Prototype-based (with class syntax on top) |
| How code runs | Compiled by javac to bytecode, run on the JVM | Run directly by an engine in the browser, Node.js or another host |
| File extension | .java, compiled to .class | .js |
| Standardised by | The Java Language Specification and JVM Specification | ECMA-262 (ECMAScript), also ISO standard |
| Native in web browsers | No: browsers run JavaScript | Yes |
| Typical use | Server back ends and large applications | Web pages, front-end apps, Node.js servers and tooling |
The same program in both languages
This is example code, not captured output. A function that adds two numbers in Java:
public class Add {
static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
System.out.println(add(2, 3));
}
}
And in JavaScript:
function add(a, b) {
return a + b;
}
console.log(add(2, 3));
Both print 5. The Java version declares that a, b and the return value are int, and everything lives inside a class. The JavaScript version has no declarations and no class, and the function accepts whatever you pass it. In JavaScript, add("2", 3) returns the string "23". In Java, passing a String where an int is expected is a compile-time error, so the program is never built.
Typing: declared versus discovered
The Java Language Specification says: "The Java programming language is strongly and statically typed." Every variable and expression has a type known before the program runs, and the compiler rejects mismatches.
MDN describes JavaScript as "a dynamic language with dynamic types": a variable is not tied to a value type and can be reassigned values of any type. It is also "a weakly typed language, which means it allows implicit type conversion when an operation involves mismatched types, instead of throwing type errors." That is why 1 + "3" gives the string "13" instead of an error.
A smaller difference follows from the same design. MDN states that JavaScript's Number type is a double-precision 64-bit binary format IEEE 754 value, so there is one general number type for integers and decimals alike. Java has separate primitive types such as int and double, which you choose between.
How each one runs
Java source is compiled before it runs. Oracle's tutorial describes the pipeline: source files ending in .java are compiled into .class files by the javac compiler, a .class file contains bytecodes (the machine language of the Java Virtual Machine, not native processor code), and the java launcher then runs the application on a JVM. Because a JVM exists for many operating systems, the same .class files can run on Windows, Linux and macOS.
javac Add.java
java Add
Since JDK 11 you can skip the explicit compile step for a single-file program: JEP 330 added support for running java Add.java directly from source. That is handy for small programs and scripts.
JavaScript has no developer-facing compile step. You hand source text to an engine, which is built into every web browser and into runtimes such as Node.js, and it executes the code. The language itself is defined by the ECMAScript standard. MDN notes that ECMAScript does not describe the Document Object Model (DOM); the DOM is standardised separately by W3C and WHATWG. So "JavaScript in a browser" is the language plus browser APIs, and the same language in Node.js comes with different APIs.
Objects and classes
Java is class-based. MDN's comparison says objects are divided into classes and instances, all inheritance goes through the class hierarchy, and classes and instances cannot have properties or methods added dynamically.
JavaScript is prototype-based: an object gets behaviour from its own properties and from its prototype's. Modern JavaScript has a class keyword, and it makes code look much like Java:
class Counter {
#count = 0;
increment() {
this.#count += 1;
return this.#count;
}
}
MDN is explicit about what this is, though: "In JavaScript, classes are mainly an abstraction over the existing prototypical inheritance mechanism". Classes are ordinary JavaScript values with their own prototype chains. Fields prefixed with # are private, and accessing one outside the class is an early syntax error.
Concurrency
Oracle's tutorial calls multithreaded execution "an essential feature of the Java platform": an application starts with a main thread, which can create additional threads. JavaScript code is structured around an event loop instead. MDN's promise guide describes callbacks being placed on a microtask queue and run when the execution stack is empty, and says await pauses only the code that depends on its value, so other asynchronous jobs can run while it waits. In practice, Java code uses threads that can run in parallel, and JavaScript code uses promises and async/await to wait on work without blocking.
Where each one is used
- Web page behaviour: JavaScript. It is the language browsers run natively.
- Server code: both. Java runs on the JVM; JavaScript runs on servers through Node.js and similar runtimes.
- Large long-lived systems: Java's declared types and compile step give tooling a lot to work with. JavaScript teams often add types with TypeScript for the same reason.
For these reasons, the two languages are often used in the same product: a Java back end serving an API and a JavaScript front end consuming it.
Which should you learn first?
Choose by what you want to build. If the goal is anything that runs in a web page, you need JavaScript regardless. If the goal is back-end engineering or a job in a company with a large Java codebase, learn Java. Neither choice locks you in: both use the same C-style syntax of braces, if, for and while, so a second language is quicker to pick up than the first.
Start with JavaScript if you want results in minutes: you can open a browser's developer console and run code with no install. Start with Java if you want the compiler to teach you about types from day one, which some learners find clearer even though the setup takes longer (a JDK and an editor).
Frequently asked questions
- Is JavaScript the same as Java?
- No. They are separate languages with different designs, runtimes and standards. MDN says JavaScript resembles Java but does not have Java's static typing and strong type checking, and that it was renamed from LiveScript to JavaScript partly because it follows most of Java's expression syntax and naming conventions. The shared name does not mean shared code or a shared platform.
- Which is harder to learn, Java or JavaScript?
- Java asks more of you at the start: you declare types, put code inside classes and compile before running. JavaScript lets you run a line in a browser console immediately, but its implicit type conversions and asynchronous behaviour cause their own confusion. Neither is objectively harder; Java front-loads the rules, JavaScript lets you discover them later.
- Can JavaScript replace Java?
- They overlap on servers, where Node.js runs JavaScript, but they are not drop-in replacements. Only JavaScript is the language of web pages in browsers, while Java runs on the JVM. Which fits depends on the platform you are targeting and the code that already exists.
- Do I need to know Java to learn JavaScript?
- No. JavaScript is a complete language on its own and does not depend on Java in any way. Knowing Java helps you recognise its C-style syntax (braces, if, for, while), but you will need to unlearn some expectations, such as declared types and class-based inheritance.
Sources
- MDN: JavaScript Guide, Introduction (JavaScript and Java)
- The Java Language Specification, Java SE 21: Chapter 1, Introduction
- Oracle Java Tutorials: About the Java Technology
- MDN: JavaScript data types and data structures
- MDN: Using classes
- JEP 330: Launch Single-File Source-Code Programs
- Oracle Java Tutorials: Processes and Threads