➥ JS Tutorial
- JS HOME
- JS introduction
- JS Where to
- JS Output
- JS Statements
- JS Syntax
- JS Variables
- JS Operators
- JS Assignment
- JS Data Types
- JS Functions
- JS Objects
- JS Events
- JS Sting methods
- JS String Search
- JS Arrays Methods
- JS Arrays
- JS Sorting Arrays
- JS Array Iteration
- JS Math Object
- JS Comparison & Logical Operators
- JS if else and else if
- JS Switch Statement
- JS For Loop
- JS For In
- JS For Of
- JS While Loop
- JavaScript Break
- JS Sets
- JavaScript typeof
- JS Type Conversion
- JS Bitwise Operations
- JS JSON
- JS Best Practices
- JS Reserved Words
- JS Number Methods
➥ JS Objects
➥ JS Functions
➥ JS Classes
➥ JS HTML DOM
- JavaScript HTML DOM
- JS HTML DOM Document
- JS HTML DOM Elements
- Changing HTML
- JS Forms
- JS HTML DOM - CSS
- DOM Events
- HTML DOM Event Listener
➥ JS Browser BOM
➥ JS Web APIs
➥ JS AJAX
➥ JS JSON
Tutorial
JavaScript Number Methods
Number methods help you work with numbers.
Number Methods and Properties
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
The toString()
method returns a number as a string.
All number methods can be used on any type of numbers (literals, variables, or expressions):
EXAMPLE ❯
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.
The parameter is optional. If you don't specify it, JavaScript will not round the number.
EXAMPLE ❯
The toFixed() Method
toFixed() returns a string, with the number written with a specified number of decimals:
EXAMPLE ❯
toFixed(2)
is perfect for working with money.
The toPrecision() Method
toPrecision()
returns a string, with a number written with a specified length:
EXAMPLE ❯
The valueOf() Method
valueOf()
returns a number as a number.
EXAMPLE ❯
In JavaScript, a number can be a primitive value (typeof = number) or an object (typeof = object).
The valueOf()
method is used internally in JavaScript to convert Number objects to primitive values.
There is no reason to use it in your code.
All JavaScript data types have a valueOf()
and a toString()
method.
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:
If the number cannot be converted, NaN
(Not a Number) is returned.
EXAMPLE ❯
The Number() Method Used on Dates
Number()
can also convert a date to a number.
EXAMPLE ❯
EXAMPLE ❯
EXAMPLE ❯
The parseInt() Method
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 ❯
The parseFloat() Method
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 ❯
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.
EXAMPLE ❯
MIN_VALUE returns the lowest possible number in JavaScript.
EXAMPLE ❯
JavaScript POSITIVE_INFINITY
EXAMPLE ❯
POSITIVE_INFINITY
is returned on overflow:
EXAMPLE ❯
JavaScript NEGATIVE_INFINITY
EXAMPLE ❯
NEGATIVE_INFINITY
is returned on overflow:
EXAMPLE ❯
JavaScript NaN - Not a Number
EXAMPLE ❯
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):
EXAMPLE ❯
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
: