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.
But in Java they are represented by two different primitive data types int and float respectively.
In Java if you try to parse a string (which has a non numerical value) as a number it would throw an exception.
Number('hello') returns NaN
If you divide a number by zero Java will throw exception.
<script> alert( 5/0); </script>
The above code alerts the value Infinity:
Infinity can be used to compare with other values:
<script> alert( 3 < Infinity); </script>
The above code returns true:
You can also use -Infinity to represent infinity in the negative direction:
<script> alert( -3 < -Infinity); </script>
The above code returns false since -Infinity represents the minimum number:
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.
<script> let x = '3'; alert( x+2); </script>
Appending ‘+’ before x changes it to a number.
The below code returns 5:
<script> let x = '3'; alert( +x+2); </script>