Number Methods Flashcards
The toString() Method The toString() method returns a number as a string.
All number methods can be used on any type of numbers (literals, variables, or expressions):
var x = 123; // returns 123 from variable x // returns 123 from literal 123 // returns 123 from expression 100 + 23
var x = 123; x.toString(); // returns 123 from variable x (123).toString(); // returns 123 from literal 123 (100 + 23).toString(); // returns 123 from expression 100 + 23
var x = 123; document.getElementById("demo").innerHTML = x.toString() + "<br>" + (123).toString() + "<br>" + (100 + 23).toString();
The toExponential() Method toExponential() returns a string, with a number rounded and written using exponential notation.
A parameter defines the number of characters behind the decimal point:
var x = 9.656; // returns 9.66e+0 // returns 9.6560e+0 // returns 9.656000e+0
var x = 9.656;
x. toExponential(2); // returns 9.66e+0
x. toExponential(4); // returns 9.6560e+0
x. toExponential(6); // returns 9.656000e+0
var x = 9.656;
document. getElementById(“demo”).innerHTML =
x. toExponential() + “<br></br>” +
x. toExponential(2) + “<br></br>” +
x. toExponential(4) + “<br></br>” +
x. toExponential(6);
The parameter is optional. If you don’t specify it, JavaScript will not round the number.
The toFixed() Method toFixed() returns a string, with the number written with a specified number of decimals:
Example var x = 9.656; // returns 10 // returns 9.66 // returns 9.6560 // returns 9.656000
toFixed(2) is perfect for working with money.
var x = 9.656;
document. getElementById(“demo”).innerHTML =
x. toFixed(0) + “<br></br>” +
x. toFixed(2) + “<br></br>” +
x. toFixed(4) + “<br></br>” +
x. toFixed(6);
The toFixed() method rounds a number to a given number of digits.
For working with money, toFixed(2) is perfect.
The toPrecision() Method toPrecision() returns a string, with a number written with a specified length:
var x = 9.656; // returns 9.656 // returns 9.7 // returns 9.656 // returns 9.65600
var x = 9.656;
document. getElementById(“demo”).innerHTML =
x. toPrecision() + “<br></br>” +
x. toPrecision(2) + “<br></br>” +
x. toPrecision(4) + “<br></br>” +
x. toPrecision(6);
Converting Variables to Numbers
There are 3 JavaScript methods that can be used to convert variables to numbers:
The Number() method The parseInt() method The parseFloat() method These methods are not number methods, but global JavaScript methods.
Global JavaScript Methods
JavaScript global methods can be used on all JavaScript data types.
These are the most relevant methods, when working with numbers:
Method Description
Number() Returns a number, converted from its argument.
parseFloat() Parses its argument and returns a floating point number
parseInt() Parses its argument and returns an integer
The Number() Method Number() can be used to convert JavaScript variables to numbers:
Example Number(true); // returns 1 Number(false); // returns 0 Number("10"); // returns 10 Number(" 10"); // returns 10 Number("10 "); // returns 10 Number(" 10 "); // returns 10 Number("10.33"); // returns 10.33 Number("10,33"); // returns NaN Number("10 33"); // returns NaN Number("John"); // returns NaN
document.getElementById("demo").innerHTML = Number(true) + "<br>" + Number(false) + "<br>" + Number("10") + "<br>" + Number(" 10") + "<br>" + Number("10 ") + "<br>" + Number(" 10 ") + "<br>" + Number("10.33") + "<br>" + Number("10,33") + "<br>" + Number("10 33") + "<br>" + Number("John");
If the number cannot be converted, NaN (Not a Number) is returned.
Number() can also convert a date to a number:
Number(new Date("2017-09-30")); // returns 1506729600000 The Number() method above returns the number of milliseconds since 1.1.1970.
The parseInt() Method parseInt() parses a string and returns a whole number. Spaces are allowed. Only the first number is returned:
parseInt("10"); // returns 10 parseInt("10.33"); // returns 10 parseInt("10 20 30"); // returns 10 parseInt("10 years"); // returns 10 parseInt("years 10"); // returns NaN
The global JavaScript function parseInt() converts strings to numbers:
document.getElementById("demo").innerHTML = parseInt("10") + "<br>" + parseInt("10.33") + "<br>" + parseInt("10 6") + "<br>" + parseInt("10 years") + "<br>" + parseInt("years 10");
If the number cannot be converted, NaN (Not a Number) is returned.
The parseFloat() Method parseFloat() parses a string and returns a number. Spaces are allowed. Only the first number is returned:
parseFloat("10"); // returns 10 parseFloat("10.33"); // returns 10.33 parseFloat("10 20 30"); // returns 10 parseFloat("10 years"); // returns 10 parseFloat("years 10"); // returns NaN
The parseFloat() method converts strings to numbers:
document.getElementById("demo").innerHTML = parseFloat("10") + "<br>" + parseFloat("10.33") + "<br>" + parseFloat("10 6") + "<br>" + parseFloat("10 years") + "<br>" + parseFloat("years 10");
If the number cannot be converted, NaN (Not a Number) is returned.
Number Properties
Property Description
MAX_VALUE Returns the largest number possible in JavaScript
MIN_VALUE Returns the smallest number possible in JavaScript
POSITIVE_INFINITY Represents infinity (returned on overflow)
NEGATIVE_INFINITY Represents negative infinity (returned on overflow)
NaN Represents a “Not-a-Number” value
JavaScript MIN_VALUE and MAX_VALUE
MAX_VALUE returns the largest possible number in JavaScript.
MIN_VALUE returns the lowest possible number in JavaScript.
JavaScript POSITIVE_INFINITY
POSITIVE_INFINITY is returned on overflow:
JavaScript NEGATIVE_INFINITY
NEGATIVE_INFINITY is returned on overflow:
JavaScript NaN - Not a Number
NaN is a JavaScript reserved word indicating that a number is not a legal number.
Trying to do arithmetic with a non-numeric string will result in NaN (Not a Number):
Number Properties Cannot be Used on Variables
Number properties belongs to the JavaScript’s number object wrapper called Number.
These properties can only be accessed as Number.MAX_VALUE.
Using myNumber.MAX_VALUE, where myNumber is a variable, expression, or value, will return undefined:
var x = Number.MAX_VALUE;
var x = Number.MIN_VALUE;
var x = Number.POSITIVE_INFINITY;
var x = 1 / 0;
var x = Number.NEGATIVE_INFINITY;
var x = -1 / 0;
var x = Number.NaN;
var x = 100 / “Apple”; // x will be NaN (Not a Number)
var x = 6; var y = x.MAX_VALUE; // y becomes undefined