- Value: 52 bits (0-51)
- Exponent: 11 bits (52-62)
- Sign: 1 bit (63)
1. For Large Numbers
You can shorten the number by simply appending the letter “e”, followed by the number of zeroes after it. Let’s use this technique to test different values in the browser console.
var million = 1e6; //1 and 6 zeroes (one million) var milliseconds = 2e-3; //5 zeroes to the left from 2
2. hex, octal and binary numbers
var decimal = 157 // decimal number 0x9D //hexadecimal number 0o235 //octal number 0b10011101 //binary number
3. The parseInt() and parseFloat() methods
The parseInt() and parseFloat() methods help us to achieve this task by extracting integers and floating-point numbers from the given value. The methods do it by reading the value, from left to right. While the parseInt() method returns the integer and stops reading the value after getting a non-number value, the parseFloat() method continues its work even after the first point. It then returns the floating-point value (with a single point) and skips everything after the floating-point data it needed.
The syntax for the parseFloat() method is as follows:
The syntax of the parseInt() method looks like this:
Why the second parameter? The optional parameter (radix) specifies the base of the numeral system, allowing us to extract numeric data from values with different bases too.
Let us see these methods in action; open up your console and get started with the code below.
var time = '2e-3s' parseFloat(time) //time in seconds // 0.002 parseInt(time) //time in milliseconds // 2 parseInt('9d', 16) //extracting the number's base 16 equivalent // 157 parseInt(0o34, 17) //extracting base 17 equivalent of octal number // 42 parseInt('ff', 8) //out of the range of octal number // NaN
Notice that we extracted the hex and octal methods with different specifications. We ignored 0x with 9d, but still got the expected result. When we tried to do the same with base 8, we got NaN. This is because numbers with base 8 don’t recognize the string as a hex number. You need to use 0xff in its place if you want to find its value.
Adding Strings and Numbers
1 + 2 //arithmetic operator // 3 //concatenation operator "4" + 1 // 41 1 + '2' // 12 //combining arithmetic and concatenation operators 1 + 2 + "4" + 2 + 1 // 3421
Since the number is represented in 64-bit format, internally, there are exactly 64 bits available to store a number. If a number exceeds this storage limit, we get an Infinity. This will cause errors and crash our application.
Another problem we might face while dealing with numbers is the loss of precision. It isn’t always obvious but happens quite a lot.
Let’s first understand what I mean with the two problems above by testing the following code in the browser console.
4e786 //problem1 // Infinity 1.6 + 0.8 //problem2 // 2.4000000000000004 1.6 + 0.8 = 2.4 //false
Okay, that wasn’t what we expected, was it? We expected the result of 1.6 + 0.8 to be 2.4. And though close, it isn’t the same; this will cause havoc in our programs. This is the reason we got false when compared with the expected result.
The parameter of this method specifies the precision i.e. number of figures on the right of the decimal. This method always returns a string, so to get a number, we can use a unary plus at the beginning.
Test the following code in the console:
var pi = 22/7 pi.toFixed(10) // "3.1428571429" var sum = 1.6 + 0.8 sum.toFixed(2) // "2.40" +sum.toFixed(2) // 2.4
isNaN() and isFinite() methods
Note: NaN === NaN returns false because the value of NaN is not equal to anything, including itself.
num1 = 120 / "DataFlair" // NaN num2 = 120 / "10" //type-casting // 12 isNaN(num1) // true isNaN(num2) // false isNaN() // true typeof NaN // "number"
Using the isFinite() method, we can check if a number is a regular number (returns true) or NaN, Infinity or -Infinity (returns false).
Try the following code in your console window to verify what we’ve discussed about Infinity.
typeof Infinity // "number" isFinite(NaN) // false isFinite("DataFlair") // false isFinite(14) // true isFinite(Infinity) // false isFinite() // false
The Number Object
The Number object is a wrapper object that works with numerical data values. Until now, we worked with numeral literals (var number = value), but we can create a Number object with the Number() constructor. The syntax of creating a Number object is as follows:
var num = new Number(value);
Properties of Number Object
The following are the properties associated with a Number object:
It is the largest positive representable number.
It represents the largest safe integer number (253 – 1).
It is the smallest positive representable number i.e. the number closest to zero without actually being zero.
It represents the smallest safe integer number (-(253 – 1)).
It equals to a special value that is “not a number”.
It is a special value representing infinity, returned on overflow.
It is a special value representing negative infinity, returned on overflow.
The program below implements the different Number properties that we discussed.
Number.MAX_VALUE // 1.7976931348623157e+308 Number.MAX_SAFE_INTEGER // 9007199254740991 Number.MIN_VALUE // 5e-324 Number.MIN_SAFE_INTEGER // -9007199254740991 Number.NaN // NaN Number.POSITIVE_INFINITY // Infinity Number.NEGATIVE_INFINITY // -Infinity
The table below lists all the methods you need to focus on.
It returns the exponential value as a string.
It returns the number as a string with precision digits in total.
It returns the string representation of the object in the specified radix.
It returns the primitive value of the specified object.
It determines whether the value passed is an integer or not.
It determines whether the value passed is a safe integer or not.
Let’s implement these methods in the console window.
num = 250 // 250 num.toExponential() // "2.5e+2" num.toExponential(2) // "2.50e+2" num.toPrecision(5) // "250.00" num.toString(17) // "ec" num.valueOf() // 250
Converting different Data Types to Numbers
Number() // 0 Number("") // 0 Number("12") // 2 Number("12.50") // 12.5 Number("125e-2") // 1.25 Number(null) // 0 Number("null") // NaN Number(0xff) // 255 Number(0o150) // 104 Number(0b110) // 6 var date = new Date() date // Fri Jul 26 2019 14:25:45 GMT+0530 (India Standard Time) Number(date) // 1564131345705
We also used the Date object in the last three statements. We will discuss what they are and how they work in the next tutorial of this series.
Hope you liked our article. Don’t forget to share your views through the comment section below.