The open blogging platform. Say no to algorithms and paywalls.

An image of a robot

Alright, this article is aiming to answer this very popular answer to newcomers in the IT world.

If you ask yourself this question you're probably starting programming and think of choosing the proper language. We will see that's not the real question.

Both are programming languages, they achieve the same goal which is to automate some tasks using programming.

What is JavaScript?

JavaScript is a lightweight programming language("scripting language") and used to make web pages interactive most of the time. You can also use JavaScript on the server-side using the very popular NodeJS.

That's the main benefit of JavaScript, it plays on various platforms: browser, server, IoT, and even mobile.

By scripting it means that code can be serialized and executed within runtime (however, there are kind of caching mechanism to optimize the runtime performances inside the engine)

What is Java?

Java is an object-oriented programming language. Java promised, "Write Once, Run Anywhere".

This is achieved thanks to a virtual machine platform that allows you to create compiled programs that run on nearly every platform.

This is a very popular language, widely used in industry, especially in banking.

Differences between JavaScript and Java

Static typing and compilation

Java is a strongly typed language, which means you should give them a type and declare them before using it. In Java, the type of a variable is checked at compile-time. Java can only run compiled with machine code and force compilation.

JavaScript has weak typings, you can change and reassign variables to different types at runtime. This also leads to common issues for beginning where you're expecting a variable to be a number whereas it's a string.

TypeScript can help in such cohesion.

JavaScript code is just text, which is interpreted and cached by the engine at runtime. This has some benefits, such as "over the air" updates or lazy loading of code

Paradigms

  • Java is Object-oriented only, all the code is written using classes and gives objects instances at runtime.
  • JavaScript on the other side is originally designed to be a functional language using functions and prototypes.

Now there is kind of classes that are not advanced like their Java version but allows of kind of Oriented Object Programming (OOP).

Using TypeScript, you can mimic the behavior of typing and encapsulation.

You have more freedom and it's also easier to play with JSON, which is the most used data format in the world. But remind to be consistent in the way you write code.

Concurrency

  • Java uses thread to manage concurrency, which can get complicated when the program grows and need to sync data between thread.
  • JavaScript uses events to manage concurrency, it's a single-threaded language and uses some kind of queue (called Event-loop) to handle operations. You can mimic multi-threading by starting the same application on multiple threads (for a web server clustering as an example)

Should I better start with Java or JavaScript?

Generally speaking, I can only suggest learning Object Oriented Programming to learn best practices of coding.

  • Java gives you a context that you need to respect and this is great to learn.

Java has overall good performances and by ensuring types it gives strong confidence in code.

You can use it server-side, on desktop, and also with Android.

  • JavaScript is wilder as you're free to do anything you want thanks to no-typings and the capability to play with classes and functions.

JavaScript power the web and almost all webpages. You can also use it server-side with NodeJS, on a desktop with Electron, or on mobile apps with something like React Native.

Both languages are really nice, you should really learn both. You can learn Java here, although it might be easier to start with JavaScript in terms of setup and knowledge.




Continue Learning