JavaScript Numbers

JavaScript has only one type of number. Numbers can be written as whole numbers known as integers, or decimal numbers known as floating-point numbers.

TIP

When assigning a number to a variable, no quotes or backticks should be used.

const myWholeNumber = 7
const myDecimalNumber = 7.77

Methods and Functions

JavaScript includes useful methods and functionsopen in new window for working with numbers. These methods will work with any type of numbers with (literals, variables, or expressions).

The toFixed() Method

The toFixed() method returns a string, with the number written with a specified number of decimals.

NOTE

If the original number contains more decimals than specified, then the additional numbers will be rounded. If the original number contains fewer decimals than specified, then zeros will be added to the end.

const pi = 3.1415
console.log(pi.toFixed(0)) // 3
console.log(pi.toFixed(1)) // 3.1
console.log(pi.toFixed(3)) // 3.142
console.log(pi.toFixed(5)) // 3.14150

The toPrecision() Method

The toPrecision() method returns a string with a number written with a specified length.

NOTE

If the original number contains more digits than specified, then the additional numbers will be rounded. If the original number contains fewer digits than specified, then zeros will be added to the end.

const a = 3.1415

console.log(a.toPrecision(1)) // 3
console.log(a.toPrecision(3)) // 3.14
console.log(a.toPrecision(5)) // 3.1415
console.log(a.toPrecision(7)) // 3.141500

The parseInt() Function

The parseInt() function returns an integer (whole number) from the value provided. The value provide can be a number, string, or array.

NOTE

The parseInt() function will return null if the first value encountered cannot be converted to a number.

console.log(parseInt(3.1415))         // 3
console.log(parseInt('3.1415'))       // 3
console.log(parseInt([3.1415]))       // 3
console.log(parseInt('pi = 3.1415'))  // null

The parseFloat() Function

The parseFloat() function returns an floating point number (decimal) from the value provided. The value provide can be a number, string, or array.

NOTE

The parseFloat() function will return null if the first value encountered cannot be converted to a number.

console.log(parseFloat(3.1415))        // 3.1415
console.log(parseFloat('3.1415'))      // 3.1415
console.log(parseFloat([3.1415]))      // 3.1415
console.log(parseFloat('pi = 3.1415')) // null

This Scrimba screencast was created by Dylan C. Israel.

References