Variables – Java vs Javascript

Variables are critical to any programming language.

It is where you store temporary data.

Not all data needs to be stored in the database, whatever doesn’t need to be stored in the database and is needed temporarily during your program execution are stored in variables.

Variables in Javascript differ from variables in Java in quite a few ways:

Static Typing vs Dynamic Typing

In Java you declare a variable based on its type:

int a;
char b;
String c;

You know what type a variable is at compile time.

But Javascript is more generous. It let’s you declare a variable of any type using “let” keyword:

For the above three declarations in Java , the equivalent Javascript declaration would be :

let a;
let b;
let c;

All the same!

Then how do you identify the type of each variable.

It is done based on the value you assign to it.

So if you assign a number to a variable it is a number , a string to a variable it is a string.

There is no “character” data type in Javascript.

So character is also considered as a string.

Now let’s assign some value to the above variables:

let a = 5;
let b = 'b';
let c = 'hello';

Now a is a variable of type ‘Number’

b and c are variables of type ‘String’

With more power comes more responsibility , so the developers have to make sure that they are not passing a string when they had to pass a number!

Javascript variables can also be declared using “var” keyword.

But it is outdated because it has few issues like:

  • You can initialize a variable before declaring it :
a = 1;

var a;
  • You can declare two variables with the same name
var a=1;
var a='hello';

Both the above are not possible through “let” keyword which makes the code more readable.

So it is advisable not to use var keyword.

oh wait!

Java 10 introduced the “var” keyword just when Javascript wanted to get rid of it.

Using this keyword Java can dynamically infer the type of data based on the context.

So ,

var i = 5;

means i is an integer

var a = “hello”;

means a is a string

But var keyword in Java has many limitations such as :

It cannot be used as a method parameter .

It cannot be used as a return type in a method.

It cannot be used a class instance variable.

It cannot be used as a global variable.

Data Types:

In Java you have a lot of data types:

Primitive data types : int , char , float , double , boolean, string, byte, short , long

Non primitive data types : Integer , Character , Double , Boolean , String , List, Map etc

Custom data types: Any class you create .

In Javascript you have 8 data types:

1. String
2. Number
3. Bigint
4. Boolean
5. Undefined
6. Null
7. Symbol
8. Object

The object data type can further be divided into three:

  • Array
  • Date
  • A custom object defined by user
//Numbers
let marks = 75;
let height = 6.2;

// Strings:
let name = "Kumar";


// Booleans
let x = true;


// Object:
const person = {firstName:"John", lastName:"Nash"};

// Array object:
const cars = ["Maruthi", "Volvo", "BMW"];

// Date object:
const date = new Date("2022-03-25");

So javascript variables can only hold these data types.

Constants:

What do you do when you want to declare a constant in Java , a variable whose value cannot be changed.

You use the same datatype variable : int for integer constants , string for string constants etc.

you then prefix the keyword “final” to it.

And you also prefix the keyword “static” to it if you want to use it without creating an object.

And you also prefix the keyword “public” to access it outside the class you declared it.

Javascript gives a brand new keyword:

“const”

All you have to do is assign any data type value to the const keyword and you get a constant:

const a = 1; // constant number
const b = "hello"; // constant string

Access Modifiers:

Java provides four access modifiers:

  • public – to access the variable outside the class and package
  • private – to access the variable only within a class
  • default – to access the variable within a package
  • protected – to access the variable within a package and from a sub class outside the package

There are no such modifiers in Javascript.

Its scope lies in its immediate block.

So if you declare a variable within a method its visibility lies within it , if it is within a class the visibility is within it , if it is within a module its visibility is within it.

Javascript has kept it simple!

Method Parameters:

In Java when you want to pass some value to a method as a parameter , the method should declare its type using a variable.

So if a function accepts an integer value as a parameter you use an integer variable:

int findSquaer(int a) {
   return a * a;

}

In Javascript , well you can completely ignore it:

function(a){

   return a*a;
}

Again as earlier mentioned with more power comes more responsibility.

Developers should make sure that they don’t pass a string to the above function!

That’s it!


Posted

in

,

by

Comments

Leave a Reply

Discover more from The Full Stack Developer

Subscribe now to keep reading and get access to the full archive.

Continue reading