Differences Between Java and JavaScript Variables

In this post, I will be giving a brief introduction to the fundamental differences between the variables in two of the most popular programming languages: Java and JavaScript.

In Java, almost every variable has a data type. For example, if a variable is a 'String', then it will be declared as "String myStringName = 'Hello';".

If it is an integer, it will be declared as "int".

Here are the primitive data types that you will see a lot in Java: byte, short, int, long, float, double, boolean, char.

So what about 'var'?

We see that a lot in JavaScript, but what does it mean in Java?

Well, as of Java 10, they added a new variable type, and you guessed it, it's 'var'.

Is it the same 'var' in JavaScript? Almost, it is.

It can be used as a String, int, float, etc. But here is the difference: only as a local variable.

You cannot use it as a global variable. In JavaScript, you can even go with "variableName = 1345" and then on the second line, you can say "variableName = 'Hey';".

This is a big difference that you should always keep in mind when working with these languages.

JavaScript automatically converts data types depending on the value you give, but Java has strict rules for it. Both have benefits and disadvantages.

June 9, 2023