How do numbers differ in Javascript and Java?

Numbers are a basic data type.

You can’t have a programming language without them.

Yet how these numbers are treated can vary slightly between different languages.

Brendan Eich , when he created Javascript was hugely influenced by Java. In face he was asked to create a Java language for the browser by Netscape and ended up creating Javascript.

Though Java and Javascript have a lot of similarities , they differ quite widely.

Here is a look at the features which Javascript has and Java does not:

All numbers in Javascript are floating point numbers:

So 5 = 5.0 in Javascript.

But in Java they are represented by two different primitive data types int and float respectively.

Not a number can be represented by a value in Javascript:

In Java if you try to parse a string (which has a non numerical value) as a number it would throw an exception.

But Javascript would return NaN (Not a number):

Number('hello') returns NaN

Javascript can represent infinity:

If you divide a number by zero Java will throw exception.

But javascript will return infinity:

alert( 5/0);


The above code alerts the value Infinity:

Infinity can be used to compare with other values:


alert( 3 < Infinity);


The above code returns true:

You can also use -Infinity to represent infinity in the negative direction:


alert( -3 < -Infinity);


The above code returns false since -Infinity represents the minimum number:

So the range of numbers in Javascript is -Infinity to Infinity!

You can convert a string to a number by using the plus’+’ operator in Javascript:

Converting a string to number is easy in Javascript.

You just need to append the plus operator before the string variable.

In Java you need to use methods like parseInt().

The below code returns the value ’32’ initially as it considers 3 and 2 as strings.


let x = '3';
alert( x+2);


Appending ‘+’ before x changes it to a number.

The below code returns 5:


let x = '3';

alert( +x+2);


That’s it!





One response to “How do numbers differ in Javascript and Java?”

  1. 20bet Avatar

    Your article gave me a lot of inspiration, I hope you can explain your point of view in more detail, because I have some doubts, thank you.

Leave a Reply

%d bloggers like this: