Javascript Numbers:
JavaScript has specific type of numbers, which can easily be written with or without decimals. The number (value) transforms a string or other component to the number type.
Example:
var x = 2.76; // A number with decimals
var y = 2; // A number without decimals
We can write huge values or extra small values with exponent notation.
Example:
var x = 123e5; // 12300000
var y = 123e-5; // 0.00123
Live Demo!
Numbers in javascript are always 64-bit floating point:
- JavaScript does not determine dissimilar types of numerals like small, huge, floating-point, integers etc.
- JavaScript numerals are always saved double accuracy floating-point numerals.
- It accompanies the international IEEE standard.
- This arrangement saves numerals in 64 bits whereas, the numeral is saved in bits 0 to 51, the component in bits 52 to 62, and the sign in bit 63:
Value | Exponent | Sign |
---|---|---|
52 bits (0 - 51) | 11 bits (52 - 62) | 1 bit (63) |
Precision
Whole numbers are precise up to 15 numbers.
EXAMPLE:
var x = 999999999999999; // x will be 999999999999999
var y = 9999999999999999; // y will be 10000000000000000
Live Demo!
The highest number of digits is 17, but floating-point arithmetic is not usually 100% precise.
Example:
var x = 0.4 + 0.2; // x will be 0.60000000000000001
Live Demo!
It also helps to multiply and divide to simplify the difficulty above:
Example:
var x = (0.4 * 10 + 0.2 * 10) / 10; // x will be 0.6
Live Demo!
ADDING NUMBERS AND STRINGS
Addition of two numbers will always return a number.
Example:
var x = 30;
var y = 40;
var z = x + y; // z will be 70 (a number)
Addition of two strings will proceed an answer that will be a string concatenation.
EXAMPLE:
var x = "30";
var y = "40";
var z = x + y; // z will be 3040 (a string)
Live Demo!
Addition of number and string will proceed a result in string concatenation.
Example:
var x = 30;
var y = "40";
var z = x + y; // z will be 3040 (a string)
Live Demo!
Addition of string and number will proceed a result in string concatenation.
EXAMPLE:
var x = "30";
var y = 40;
var z = x + y; // z will be 3040 (a string)
Live Demo!
There can be a few misunderstandings related to concatenation in Javascipt. Let's have a look:
EXAMPLE:
var x = 30;
var y = 40;
var z = "The result is: " + x + y;
Live Demo!
A usual misunderstanding is to expect the above answer to be 70.
EXAMPLE:
var x = 30;
var y = 40;
var z = "10";
var result = x + y + z;
Live Demo!
Another misunderstanding can be to expect the above code to return 304010.
NOTE:
- The JavaScript exponent runs from left side to the right side.
- We added 30 + 40 first because x and y are both numerals.
- Z is a string, that’s why 30 + "30" is concatenated.
- The + operator is applied to concatenate the strings.
Numeric Strings
JavaScript strings also includes numeral content.
var x = 200; // x is a number
var y = "200"; // y is a string
This is how JavaScript transforms strings to numerals in all numeric operations:
var x = "200";
var y = "20";
var z = x / y; // z will be 10
Live Demo!
NaN (NOT A NUMBER)
- NaN indicates that a number is an unauthorized number.
- Solving with a non-numeric string will display the answer in NaN.
Example:
var x = 200 / "Orange"; // x will be NaN (Not a Number)
Live Demo!
But, if the string consists of a digit, the result will be a numeral:
Example:
var x = 200 / "20"; // x will be 10
Live Demo!
The use of global JavaScript function isNaN() is very efficient to determine whether the value is a number:
Example:
var x = 200 / "Apple";
isNaN(x); // returns true because x is Not a Number
Live Demo!
Moreover, if you use NaN in calculation it will display the answer NaN.
Example:
var x = NaN;
var y = 7;
var z = x + y; // z will be NaN
Live Demo!
The result can be concatenated:
Example:
var x = NaN;
var y = "7";
var z = x + y; // z will be NaN7
Live Demo!
NaN is a numeral: typeof NaN restores number:
Example:
typeof NaN; // returns "number"
INFINITY
Infinity is the digit JavaScript will return if you compute a numeral greater than the largest viable number, let’s take an example.
Example:
var myNumber = 3;
while (myNumber != Infinity) { // Execute until Infinity
myNumber = myNumber * myNumber;
}
Live Demo!
Dividing numerals by 0 (zero) also results in infinity:
Example:
var x = 3 / 0; // x will be Infinity
var y = -3 / 0; // y will be –Infinity
Live Demo!
Infinity is a numerical value however, typeof Infinity restores number.
Example:
typeof Infinity; // returns "number"
Live Demo!