Java and JavaScript

Before we continue, it’s helpful to look at some key differences between Java and JavaScript (which all TypeScript programs compile to.)

Main Differences Between Java and JavaScript

Both JavaScript and Java are among the most popular languages in the world. While the two share a common name, in reality the two are very different programming languages. Java is commonly used for building standalone application such as web servers and backend systems. JavaScript is primarily used for building web applications and websites, as it can be run in a web browser and is used to create interactive features on websites. Here are some differences between the two languages:

  1. Syntactical differences: While their syntaxes may look similar, there are many differences. For example, in JavaScript, semicolons are optional.

  2. Java is a compiled language. You write code that is transformed into bytecode. The bytecode is understood and executed on a Java Virtual Machine (JVM). On the other hand, JavaScript is an interpreted language: the code doesn’t get compiled, rather it is executed as the program runs by the browser or a JavaScript run-time environment.

  3. Java is a statically-typed language, which means that the type of a variable must be declared when it is defined. JavaScript is a dynamically-typed language, which means that the type of a variable is determined at runtime.

  4. Java is an object-oriented, which means that uses classes and objects to define data and behaviors. On the other hand JavaScript is a functional programming language, which means that it uses “Functions” as its basic building blocks. (Although as we see, this gap is narrowing as both languages introduce more features to support both paradigms.)

Objects in Java and JavaScript

Both languages support the concept of Objects, however each understand and treat objects very differently.

In JavaScript, an object is a collection of properties, where a property is an association between a name (or key) and a value. A property’s value can be a function, in which case the property is known as a method. For example:

let person = {
  name: 'John',
  age: 25,
  greet: function() {
    console.log(`Hello I'm ${}`);

In the example above, the person object has three properties: name, age, and greet. The name and age properties are simple key-value pairs, while the greet property is a function.

JavaScript objects are similar to Java objects in that they both have properties and methods, but there are some key differences between the two. Objects are created in Java using a class, which is a blueprint for creating objects. In JavaScript, objects can be created directly using object literals or by using constructors.

Another difference is that all objects in Java have a specific type (its class). JavaScript objects have no type (in fact values and properties can be added to an object at any time.) As long as an object contain required properties or methods that a function needs, it can be used (any extra properties or methods are ignored.)

Free Question and Answers with Detailed Explanations

What is Duck Typing?

Variables and Data Types

Variables are used to store data values in both Java and JavaScript.

  1. Data types: In Java, all variables must be declared with a specific data type, such as int, double, or String. JavaScript is a dynamically typed language, which means that variables do not have a data type and can be assigned and re-assigned values of any data type. For example:

     let myVar = "bar"; // myVar is now a string
     myVar = 10;        // myVar is now a number
     myVar = true;      // foo is now a boolean
  2. Declaration: In Java, variables must be declared before they can be used. This means that you must specify the data type and the name of the variable when you create it. In JavaScript, you can use variables without declaring them first (but it is good practice to declare them using the var, let, or const keyword.)

  3. Scope: In Java, variables can be either local or instance (class) variables. Local variables are defined within a method and are only accessible within that method. Instance variables are defined within a class and are accessible from any method within the class. In JavaScript, variables can be either global or local. Global variables are defined outside of any function and are accessible from anywhere in the code. Local variables are defined within a function and are only accessible within that function.

For memory management, both Java and JavaScript use garbage collection to automatically free up memory that is no longer being used. In Java, the Java Virtual Machine (JVM) is responsible for managing memory and performing garbage collection. In JavaScript, the browser or JavaScript engine is responsible for managing memory and performing garbage collection. For more information on Garbage Collection in JavaScript, see this.

Here are common data types in Java and JavaScript:


  • int: Integer data type that represents a 32-bit signed integer value.
  • long: Integer data type that represents a 64-bit signed integer value.
  • float: Floating-point data type that represents a single-precision 32-bit IEEE 754 floating point value.
  • double: Floating-point data type that represents a double-precision 64-bit IEEE 754 floating point value.
  • char: Character data type that represents a single 16-bit Unicode character.
  • boolean: Boolean data type that can only have the values true or false.


JavaScript has 7 primitive data types (not an object and hence has no methods or properties)

  • number: This represents both integers and floating-point values. In JavaScript, all numbers are stored as 64-bit floating-point values.
  • bigint: Represents represent integers in the arbitrary precision format.
  • string: Represents a sequence of characters.
  • boolean: Represents a logical value and can only have the values true or false.
  • null: Represents the absence of a value or a null reference.
  • undefined: Represents the absence of a value or a declared variable that has not been assigned a value.
  • symbol: This data type is a unique and immutable data type that can be used as an identifier for objects.

The object data type is considered a complex data type in JavaScript.

Functional Programming

Functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions. In it, functions are treated as first class citizens, which means that:

  • they can be passed as arguments to other functions,
  • returned as values from functions, and ,
  • assigned to variables.

This allows for the creation of higher-order functions, which are functions that operate on other functions.

Free Explanation of Computer Science Concepts with Examples

Higher Order Functions

Functional programming also emphasizes immutability, which means that once a value has been created, it cannot be modified. This can make it easier to reason about the behavior of a program and helps avoid unintended side effects.

JavaScript is a functional programming language. While Java is primarily Object-oriented language. However, both Java and JavaScript support functional programming:


  • Java does not have built-in support for functional programming, but it supports functional interfaces and lambda expressions, which allow for the creation of anonymous functions.
  • Java has Lambda expressions that are anonymous functions that can be passed around as if they were values. They are often used to implement functional interfaces.
  • Functional interfaces are interfaces that have a single abstract method, and they are used as the target type for lambda expressions.


  • JavaScript is a functional programming language and has first-class functions, which means that functions can be treated as values and passed as arguments to other functions.
  • JavaScript also has higher-order functions, which are functions that operate on other functions.
  • JavaScript has a number of built-in higher-order functions, such as map, filter, and reduce, which can be used to transform and manipulate collections of data.
  • JavaScript also has support for arrow functions (() => { ... }), which are a shorthand syntax for defining anonymous functions.


In this chapter, we learned that Java and JavaScript are very different languages.

Overall TypeScript is more similar to Java than JavaScript in that it has support for static typing and class-based object-oriented programming. It is easier to learn for developers coming from other languages with static typing, such as C# and Java.

However, they are still different languages. Some differences between TypeScript and Java include:

  1. Data types: Both TypeScript and Java have support for different data types, but the specific data types and their characteristics are different in each language.
  2. Syntax: TypeScript and Java have different syntax and use different keywords and operators.
  3. Execution environment: Java code is typically compiled to bytecode and executed by the Java Virtual Machine (JVM), while TypeScript code is transpiled (translated and compiled) to JavaScript and executed by a JavaScript engine.

Licenses and Attributions

Speak Your Mind