- Each number method returns a new value instead of changing the one being used for the method.
Methods Used For Numbers
These are the go-to methods while working with numeric values:
|Number()||Return number converted from its argument|
|parseFloat()||Parse its argument and return a float|
|parseInt()||Parse its argument and returns an integer|
|toString()||Return number as string|
|toExponential()||Return string with number rounded and written with exponential notation.|
|toFixed()||Return string with number rounded and written using a specified number of decimals.|
|toPrecision()||Return string with a number written with a specified length|
|valueOf()||Return number as a number|
toString() turns the numeric value into a string (a sequence of characters). It can be used with literals, variables or expressions:
var a = 653; a.toString(); (653).toString(); (600 + 53).toString();
toExponential() turns numbers into strings as well, but in addition to that, the number that is returned is also rounded and written with exponential notation.
You also have access to a parameter, which is used to define the number of digits after a decimal point:
var a = 4; a.toExponential(); a.toExponential(2); a.toExponential(4); a.toExponential(6);
toFixed() also turns the number into a string, but with a specific amount of decimals this time. The number of decimals should be specified in the parentheses after the method's name:
var a = 4.236; a.toFixed(0); a.toFixed(1); a.toFixed(3); a.toFixed(5);
Note: This method is useful for working with payments, where you only need two decimals after the decimal point.
toFixed() method but counts all numbers, not only decimals.
See how it works:
var x = 4.236; x.toPrecision(); x.toPrecision(2); x.toPrecision(4); x.toPrecision(6);
- Easy to use with a learn-by-doing approach
- Offers quality content
- Gamified in-browser coding experience
- The price matches the quality
- Suitable for learners ranging from beginner to advanced
- Free certificates of completion
- Focused on data science skills
- Flexible learning timetable
- Simplistic design (no unnecessary information)
- High-quality courses (even the free ones)
- Variety of features
- Nanodegree programs
- Suitable for enterprises
- Paid Certificates of completion
Converting Variables to Numbers
If you find yourself in need to turn a variable into a number, these are the three methods you can use:
NaN (Not a Number).
Number(true) Number(false) Number("12") Number("12.24") Number("12 24") Number("12,24") Number("Tim")
Note: if this value is used on a Date() object, this method will return the amount of milliseconds that have passed since the date of 01.01.1970. This date is picked by Unix engineers as a uniform starting date for computers and programs.
parseInt(). It works by parsing a string and then returning the number. Spaces may be present in the string, but only the first number will be returned.
parseInt("21"); parseInt("21.32"); parseInt("21 32 42"); parseInt("21 days"); parseInt("day 21");
parseFloat() method works by parsing a string and then returning a floating point number. Spaces may be present in the string, but only the first number will be returned.
parseFloat("21"); parseFloat("21.14"); parseFloat("21 14 34"); parseFloat("21 day"); parseFloat("day 21");
typeOf = number) or objects (
typeOf = object). The method called
valueOf() is used internally to turn number objects into primitive values.
var a = 246; a.valueOf(); (246).valueOf(); (200 + 46).valueOf();
- Most popular methods are