Work With Numbers in JavaScript II
12 min read·Jan 1, 2026
In JavaScript, beyond numeric data, the Number type is also used to represent special values like NaN, Infinity, or -Infinity.
Convert expressions to numbers
To convert a string or an expression into an integer or a floating point number, you can use the global built-in Number function:
Number(expression)
Note that if the value can't be converted into a valid number, the Number function will return NaN.
Example
In this example, all values will be converted to numbers:
Number(1) // 1
Number('123') // 123
Number(null) // 0
Number(true) // 1
Number(false) // 0
However, these values will be converted to NaN:
Number('text') // NaN
Number(undefined) // NaN
Convert strings to integers
To convert a string to an integer, you can use the global built-in parseInt function:
parseInt(string, base?)
Where:
stringis a string starting with an integer.baseis an optional number between2and36representing the mathematical base. Defaults to10.
Notes:
- The
parseIntfunction will automatically ignore whitespaces, leading zeros, and remove the decimal part.- The
parseIntfunction will return theNaNvalue if the value cannot be converted into a number.
Example
In this example, all strings will be converted to numbers:
parseInt('123') // 123
parseInt(' 123 ') // 123
parseInt('077') // 77
parseInt('1.9') // 1
parseInt('0xff', 16) // 255
However this string will be converted to NaN:
parseInt('xyz') // NaN
Convert strings to floating point numbers
To convert a string to a floating point number, you can use the global built-in parseFloat function:
parseFloat(string)
Notes:
- The
parseFloatfunction doesn't support non-decimal literals starting with0x,0b, or0o, and will return0.- The
parseFloatfunction will return theNaNvalue if the value cannot be converted into a number.
Example
In this example, all strings will be converted to floating point numbers:
parseFloat('123') // 123
parseFloat(' 123 ') // 123
parseFloat('1.9') // 1.9
parseFloat('0xff') // 0
However this string will be converted to NaN:
parseFloat('xyz') // NaN
Not-a-number
In JavaScript, NaN (Not-a-Number) is a special value that results from arithmetic operations or expressions that don't yield a valid number.
Example
In this example, the following expressions all evaluate to NaN:
// Dividing zero by zero
0 / 0
// Performing operations where JavaScript fails to coerce types
"text" - 2
// Performing operations involving undefined
1 + undefined
// Performing operations involving NaN
NaN + 5
Checking if a value is a number
To test whether an expression is a valid number or not, you can use the built-in global isNaN function as follows:
isNaN(expression)
Upon execution, the isNaN function first converts the expression to a number and then returns true if the resulting value is not a number, or false otherwise.
Infinity
In JavaScript, Infinity and -Infinity represent values that are beyond the largest or smallest finite number that can be represented.
Example
For example, the following expressions all evaluate to Infinity or -Infinity:
1 / 0 // Infinity
-1 / 0 // -Infinity
Number.MAX_VALUE * 2 // Infinity
Number("1e309") // Infinity
Checking if a value is finite
To test whether an expression is a finite number, you can use the built-in global isFinite function.
isFinite(expression)
Upon execution, the isFinite function first tries to convert the expression to a number, and then either returns false if the conversion results in NaN, Infinity, or -Infinity, or true otherwise.
Adjusting numbers length
Adjusting the number of decimals
To trim a floating point number to a fixed number of decimals, you can use its toFixed method.
number.toFixed(digits)
Where digits is the number of decimals you want to keep.
Example
For example:
1.2345.toFixed(2) // 1.23
Adjusting the number of digits
To trim an integer or a floating point number to a fixed number of digits, you can use its toPrecision method.
number.toPrecision(digits);
Where digits is the number of digits you want to keep.
Example
For example:
1.2345.toPrecision(2) // 1.2
The built-in Math library
JavaScript provides a built-in Math object for more advanced mathematical operations.
Rounding numbers
To round a number to the nearest integer up, you can use the ceil static method.
Math.ceil(number)
To round a number to the nearest integer down, you can use the floor static method.
Math.floor(number)
To round a number to the nearest integer, you can use the round static method.
Math.round(number)
Example
For example:
Math.ceil(1.2) // 2
Math.floor(1.6) // 1
Math.round(1.2) // 1
Generating random numbers
To generate a random number between 0 (included) and 1 (excluded), you can use the random static method.
Math.random()
Example
Let's consider this script:
function getRandomNumber(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
console.log(getRandomNumber(0, 1));
console.log(getRandomNumber(1, 10));
console.log(getRandomNumber(1, 100));
When executed, it will execute the getRandomNumber function to return integers in the range specified by the min and max values.
Which will produce this output:
1
6
82
Summary
Here's a summary of what you've learned in this lesson:
- The global
Numberfunction is used to convert expressions into numbers. - The global
parseIntfunction is used to convert strings to integers. - The global
parseFloatfunction is used to convert strings to floating point numbers. - The
NaN(Not-a-Number) is a value resulting from arithmetic operations or expressions that don't yield a valid number. - The global
isNaNfunction is used to check if an expression is a valid number. - The
Infinityand-Infinityrepresent values that are beyond the largest or smallest finite number that can be represented. - The global
isFinitefunction is used to check if an expression is a finite number. - The
toFixedmethod is used to trim a floating point number to a fixed number of decimals. - The
toPrecisionmethod is used to trim an integer or a floating point number to a fixed number of digits. - The
Math.ceilmethod is used to round a number to the nearest integer up. - The
Math.floormethod is used to round a number to the nearest integer down. - The
Math.roundmethod is used to round a number to the nearest integer. - The
Math.randommethod is used to generate a random number between0(included) and1(excluded).
Enjoying the courses?
I've made these courses completely free so anyone can learn from them. If they've helped you and you'd like to actively support the work behind BackendBrewery, you can leave a tip:
Support BackendBrewery