In the last tutorial, we learned about JavaScript numbers and how to define them. When we use the simple number at the right side of the assignment operator, it will be treated as a primitive number, but if we use the JavaScript Number() object to create a number, it will be treated like a Number object.
Example
<script> let p_num = 20; // primitive number let o_num = new Number(20); // Number object console.log(typeof p_num); // number console.log(typeof o_num); // object </script>
JavaScript Number methods and properties
Primitive number values like 1, 2, 3, etc., do not support methods and properties because methods and properties are part of objects, and only an object variable can access them. However, there are built-in methods and properties associated with the JS
Number()
object, and in the background, JS also makes sure that during runtime the same properties and method are also available for the primitive number values. This means all the properties and methods that are defined for
Number()
objects can also be accessed through primitive number values. In this JavaScript tutorial, we will learn all the major number methods that you should know.
JS Number toString() method
As the name suggests, the toString() method converts the number into a string and returns it. Example
<script> let num = 200; let str_num = num.toString(); // string console.log(str_num); // 200 console.log(typeof str_num); // string </script>
By default, the toString() method converts the number into a decimal string of base 10, but we also specify the base number as a parameter for number conversion.
<script> let num = 200; let binary_num = num.toString(2); // binary let octal_num = num.toString(8); //octal let decimal_num = num.toString(10); //decimal(default) let hexa_num = num.toString(16); //hexa console.log(binary_num); //11001000 console.log(octal_num); // 310 console.log(decimal_num); // 200 console.log(hexa_num); //c8 </script>
JS Number toExponential() method
The
toExponential()
method convert the number into an exponential format or notation and return it as a string. This method also accepts an integer value as an optional parameter that defines the number of characters after the decimal point.
Example
<script> let num = 91.23; let ex_1 = num.toExponential(); let ex_2 = num.toExponential(4); console.log(ex_1); //9.123e+1 console.log(ex_2); //9.1230e+1 </script>The
num.toExponential();
statement converts the
91.23
number into
9.123e+1
which is equivalent to
9.123 * 10
.
JS Number toFixed() method
The
toFixed()
method determines how many decimal point numbers should be placed after the number. Similar to the toString() and toExponential() method it also return a string.
toFixed()
method also accepts an optional parameter which default value is 0 which represents there should be no number after the decimal point and round the number if possible.
Example
<script> let num = 91.232345; let fix1 = num.toFixed() let fix2 = num.toFixed(0) let fix3 = num.toFixed(2) let fix4 = num.toFixed(4) console.log(fix1); //91 (string) console.log(fix2); //91 (string) console.log(fix3); //91.23 (string) console.log(fix4); //91.2323 (string) </script>
toFixed() number comes in very handy when we want to specify fix number of decimal point numbers.
JS Number toPrecision() method
The
toPrecision()
method returns a specified length of number in a string format.
Example
<script> let num = 91.232345; let len1 = num.toPrecision(1); // 1 digit long let len2 = num.toPrecision(2); // 2 digit long let len4 = num.toPrecision(4); // 4 digit long console.log(len1); // 9e+1 = 9*10 console.log(len2); // 91 console.log(len4); // 91.23 </script>The parameter value of
toPrecision()
must between 1 to 100, else it throw an error.
JS Number valueOf() method
The
valueOf()
method the value of the variable. It generally used to convert the Number object to a primitive number value.
Example
<script> let o_num = new Number(92.23234) let p_num = o_num.valueOf(); console.log(p_num); // 92.23234 console.log(typeof p_num) // number </script>
Number conversion in JavaScript
There are three Global methods present in JavaScript that can be used to convert a value to a number data type value.
- Number()
- parseFloat()
- parseInt()
Number() Method
JavaScript provides a global method
Number()
that can convert any legal number value to a primitive number. Do not confuse the global
Number()
method with
new Number()
object both are different. The
Number()
method can be very useful when you want to convert a JavaScript number string to a number data type for arithmetic operations.
Example
<script> console.log(Number(true)); // 1 console.log(Number(false)); //0 console.log(Number("112")); //112 console.log(Number(" 112")); //112 console.log(Number("112 ")); //112 console.log(Number("112.23")); //112.23 console.log(Number("112,234")); // NaN console.log(Number("112 123")); //NaN console.log(Number("112J")); // NaN console.log(Number("112e1")); // 1120 console.log(Number(new Date("2021-02-31"))); // 1614729600000 </script>
Behind the code
The
Number()
method converts the true and false to the corresponding 1 and 0 integer values. It also converts the Date object into Unix timestamp seconds. And if the specified argument is not a legal representation of number it returns NaN.
parseInt() Method
As the name suggests the
parseInt()
method is used to convert a number-like value to a primitive whole number. It converts the specified number-like value to an integer value. It parses the value from left to right and converts the value into a number if possible. It will return NaN if the passed value is not a legal number from left to right.
Example
<script> console.log(parseInt(true)); // NaN console.log(parseInt(false)); // NaN console.log(parseInt(-1)); // -1 console.log(parseInt(2.23)); // 2 console.log(parseInt("-2.32")); // -2 console.log(parseInt("2 Years")); // 2 console.log(parseInt("year 2")); // NaN console.log(parseInt("2 10 20")); // 2 console.log(parseInt(new Date("2021-03-13"))); // NaN </script>
Behind the code
Unlike the
Number()
method
parseInt()
method can not convert the boolean and date object into corresponding integer values so it returns NaN instead.
parseFloat() Method
The
parseFloat()
method converts the number-like value into a floating-point number. As JS does not have a dedicated data type for float the parseFloat() method converts the passed argument to the primitive number data type. If
parseFloat()
is not able to parse the value into a number, it simply returns NaN.
<script> console.log(parseFloat(true)); // NaN console.log(parseFloat(false)); // NaN console.log(parseFloat(-1)); // -1 console.log(parseFloat(2.23)); // 2.32 console.log(parseFloat("-2.32")); // -2.32 console.log(parseFloat("2 Years")); // 2 console.log(parseFloat("year 2")); // NaN console.log(parseFloat("2 10 20")); // 2 console.log(parseFloat(new Date("2021-03-13"))); // NaN </script>
For most cases, we use
parseFloat()
method to convert a JavaScript string value to a number value, but it does not work with boolean and Date objects. So as an alternative we can also use the
Number()
method to convert a string number to a primitive number data type.
Number Properties
There are few properties that are built-in to the Number object such as
-
MAX_VALUE
: Return the possible largest number. -
MIN_VALUE
: Return the possible minimum number. -
POSITIVE_INFINITY
: Return the Positive Infinity -
NEGETIVE_INFINITY
: Return the negative Infinity -
NaN
: Return the Not a Number value.
Example
<script> console.log(Number.MAX_VALUE); // 1.7976931348623157e+308 console.log(Number.MIN_VALUE); //5e-324 console.log(Number.POSITIVE_INFINITY); //Infinity console.log(Number.NEGATIVE_INFINITY);//-Infinity console.log(Number.NaN); //NaN </script>
All these above 5 properties are available for
Number
. We can not access these properties with normal variables.
Example
let a = 23; a.MAX_VALUE; //undefined
Summary
- Mostly all the methods and properties are available for both Number objects and number data types.
- toString(), toExponential() and toFixed() methods return string numeric value.
- In JavaScript, to convert a string value to a number, we can use either of these three global methods, Number(), parseInt(), or parseFloat().
- All these three methods are not number methods. These are global methods.
- Properties like MAX_VALUE, MIN_VALUE, POSITIVE_INFINITY, NEGETIVE_INFINITY and NAN can only be accessed using the Number keyword.
People are also reading:
Leave a Comment on this Post