➢Number methods help you work with numbers.
➢Primitive values (like 3.14 or 2014), cannot have properties and methods (because they are not objects).
➢But with JavaScript, methods and properties are also available to primitive values, because JavaScript treats primitive values as objects when executing methods and properties.
➢The toString() method returns a number as a string.
EXAMPLE:
let x = 123;
x.toString();
(123).toString();
(100 + 23).toString();
➢toExponential() returns a string, with a number rounded and written using exponential notation.
EXAMPLE:
let x = 9.656;
x.toExponential(2);
x.toExponential(4);
x.toExponential(6);
➢toFixed() returns a string, with the number written with a specified number of decimals:
EXAMPLE:
let x = 9.656;
x.toFixed(0);
x.toFixed(2);
x.toFixed(4);
x.toFixed(6);
➢toPrecision() returns a string, with a number written with a specified length:
EXAMPLE:
let x = 9.656;
x.toPrecision();
x.toPrecision(2);
x.toPrecision(4);
x.toPrecision(6);
➢valueOf() returns a number as a number.
EXAMPLE:
let x = 123;
x.valueOf();
(123).valueOf();
(100 + 23).valueOf();
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.
➢JavaScript global methods can be used on all JavaScript data types.
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 |
➢Number() can be used to convert JavaScript variables to numbers:
EXAMPLE:
Number(true);
Number(flase);
Number("5");
Number(" 5");
Number("5 ");
Number(" 5 ");
Number("5.55");
Number("5,55");
Number("5 55");
Number("john");
➢Number() can also convert a date to a number.
EXAMPLE:
Number(new Date("2000-01-01"));
➢parseInt() parses a string and returns a whole number. Spaces are allowed. Only the first number is returned.
➢If the number cannot be converted, NaN (Not a Number) is returned.
EXAMPLE:
parseInt("-10");
parseInt("-10.33");
parseInt("10");
parseInt("10.33");
parseInt("10 20 30");
parseInt("10 years");
parseInt("years 10");
➢parseFloat() parses a string and returns a number. Spaces are allowed. Only the first number is returned.
➢If the number cannot be converted, NaN (Not a Number) is returned.
EXAMPLE:
parseFloat("10");
parseFloat("10.33");
parseFloat("10 20 30");
parseFloat("10 years");
parseFloat("years 10");
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 |
➢MAX_VALUE returns the largest possible number in JavaScript.
➢MIN_VALUE returns the lowest possible number in JavaScript.
EXAMPLE:
let x = Number.MAX_VALUE;
let x = Number.MIN_VALUE;
➢POSITIVE_INFINITY is returned on overflow.
EXAMPLE:
let x = Number.POSITIVE_INFINITY;
let x = 1 / 0;
➢NEGATIVE_INFINITY is returned on overflow.
EXAMPLE:
let x = Number.NEGATIVE_INFINITY;
let x = -1 / 0;
➢NEGATIVE_INFINITY is returned on overflow.
➢NaN is a JavaScript reserved word indicating that a number is not a legal number.
➢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: