Learning Javascript from Java
By: Johnny Lai
Tags: Java, Javascript Tips, Programming, Learning Javascript, ES6

If you are a Java developer and you want to learn about modern Javascript, then this page may give you some idea. Some programming concepts/syntax are similar in both languages, some are not. I try to list out the difference below in simple English

Class

In Java, everything is required to contain in Class.

In Javascript, you’re not necessarily contain everything in Class. If you only have a few lines of code, you can just put everything in the global scope. Although in React.JS, Class was important to impose specific control in a different stage of the lifecycle, but since the introduction of React Hooks, you are able to do it in functional component too.

2. “this” keyword

In Java, “this” means the current object, mostly we use “this” because the parameter pass in have the same name as class attributes.

In Javascript, “this” is much more complicated. Please read my another post — ES6 JavaScript — “this” keyword

3. Passing function as argument

For most Java Programmer, they normally understand the program by tracing the variable, find out where it define, initialize and pass in that variable. However, in Javascript, this is not always working, it is very common to pass function as a parameter and also create this function on-the-fly.

const prices = [12, 13, 5, 2.59];

const filteredArray = prices.filter((price, index, prices) => {

return price > 10;

});

console.log(filteredArray);

In the above code snippet, we created a new array by filter those value > 10.

(price, index, prices) => {

return price > 10;

}

We do this by create and pass in a new anonymous function.

Before I really start learning Javascript, I try to understand this code snippet by tracing where it define “price” and “index”, of coz Javascript is not working in that way

Actually this is a new function we just create, it is used to check each element in prices array “price”/ “index” are the arguments we assign with the own name which mean each element in the array we are processing and the index number of the array we are processing. “prices” of coz refer to the array at the first line.

const prices = [12, 13, 5, 2.59];

const filteredArray = prices.filter((a, b, prices) => {

return a > 10;

});

console.log(filteredArray);

With different parameter names in the annoymous function, it is still working, because this is a new function we define it here.

4. Multi-thread vs Single-thread

Java is a language with multi-threaded language which allow different parts of program run at the same time.

Javascript is a single-threaded language, but how about the callback function? Why Javascript can continue to execute the code while executing other callback function at the background?

Javascript is a single-threaded language but it is not doing all the task alone. Actually, Javascript will “outsource” those callback task to the browser, the browser execute it. Javascript engine will show the result when it is free (finish all other tasks) and the callback tasks are finished by browser.

Besides, HTML also offered Web Worker API, it can running Javascript in a separate thread. It is suitable for tasks require more time and resources.

In conclusion, Javascript is a single-threaded language but it doesn’t mean that Javascript operating in a single-threaded mode.

5. Truthy and Falsy value

A value that is considered true when encountered in a Boolean context.

Anything in Javascript will be treated as true or false

1. All numbers, including negative number are true, except zero is false

2. All string are true except empty string is false

3. All objects (including array) are true

4. Undefined, null, Nan are false

6. Operators

In Java

|| logical OR

&& logical AND

In Javascript

|| logical OR, also returns the first truthy value or the last one if truthy value not exist

&& logical AND, also returns the first falsy value or the last one if falsy value not exist

In Java, we can only compare boolean expression

In Javascript, everything treated as true or false, so we can comparison like below

Only available in Javascript false && "dog"

// false



0 && "dog"

// 0

While in Java, they’re invalid statement and causing compiler error