Yogesh Chauhan's Blog

JavaScript Number Methods

in JavaScript on September 7, 2020

JavaScript Number Methods

All number methods can be used on any type of numbers (literals, variables, or expressions):

isFinite()    

Checks whether a value is a finite number.

This method returns true if the value is of the type Number, and equates to a finite number. Otherwise it returns false.

Number.isFinite() is different from the global isFinite() function.

The global isFinite() function converts the tested value to a Number, then tests it.

Number.isFinite() does not convert the values to a Number, and will not return true for any value that is not of the type Number.

Examples


Number.isFinite(1) //true
Number.isFinite(-1.1) //true
Number.isFinite(4-2) //true
Number.isFinite(0) //true
Number.isFinite('1') //false
Number.isFinite('String') //false
Number.isFinite('2020/5/5') //false
Number.isFinite(Infinity) //false
Number.isFinite(-Infinity) //false
Number.isFinite(0 / 0) //false

isInteger()   

Checks whether a value is an integer.

This method returns true if the value is of the type Number, and an integer (a number without decimals). Otherwise it returns false.

Examples


Number.isInteger(1) //true
Number.isInteger(-1) //true
Number.isInteger(4-2) //true
Number.isInteger(0) //true
Number.isInteger(0.9) //false
Number.isInteger('1') //false
Number.isInteger(false) //false
Number.isInteger(Infinity) //false
Number.isInteger(-Infinity) //false
Number.isInteger(0 / 0) //false

isNaN()    

Checks whether a value is Number.NaN (not a number)

This method returns true if the value is of the type Number, and equates to NaN. Otherwise it returns false.

Number.isNaN() is different from the global isNaN() function.

The global isNaN() function converts the tested value to a Number, then tests it.

Number.isNaN() does not convert the values to a Number, and will not return true for any value that is not of the type Number.

Examples


Number.isNaN(1) //false
Number.isNaN(-1.99) //false
Number.isNaN(4-2) //false
Number.isNaN(0) //false
Number.isNaN('1') //false
Number.isNaN('str') //false
Number.isNaN('2020/5/5') //false
Number.isNaN('') //false
Number.isNaN(true) //false
Number.isNaN(undefined) //false
Number.isNaN('NaN') //false
Number.isNaN(NaN) //true
Number.isNaN(0 / 0) //true

isSafeInteger()    

Checks whether a value is a safe integer

A safe integer is an integer that can be exactly represented as an IEEE-754 double precision number (all integers from (253 – 1) to -(253 – 1))

This method returns true if the value is of the type Number, and a safe integer. Otherwise it returns false.

Examples


Number.isSafeInteger(1) //true
Number.isSafeInteger(-1) //true
Number.isSafeInteger(4-2) //true
Number.isSafeInteger(0) //true
Number.isSafeInteger(0.5) //false
Number.isSafeInteger(Math.pow(2, 53)) //false
Number.isSafeInteger(Math.pow(2, 53) - 1) //true
Number.isSafeInteger('1') //false
Number.isSafeInteger(false) //false
Number.isSafeInteger(Infinity) //false
Number.isSafeInteger(-Infinity) //false
Number.isSafeInteger(0 / 0) //false

toExponential(x)    

Converts a number into an exponential notation

Example


var number = 1.23456789;
console.log(number.toExponential());

//1.23456789e+0

toFixed(x)  

The toFixed() method converts a number into a string, rounding to a specified number of decimals.

The parameter is optional. If you don’t specify it, JavaScript will not round the number.

Example


var number = 1.23456789;
console.log(number.toFixed(2));
console.log(number.toFixed(3));
console.log(number.toFixed(4));

//1.23
//1.235
//1.2346

I know there is a 5 instead of 4 but that’s what it’s about. The function rounds up the number and if the next number is 5 or greater than it will increase it by one.

toLocaleString()  

Converts a number into a string, based on the locale settings

Examples


//Arabic (Saudi Arabia)

var number = new Number(1111111).toLocaleString("ar-SA");
console.log(number); // ١٬١١١٬١١١

//Bangla (India)

var number = new Number(1111111).toLocaleString("bn-BD");
console.log(number);  //১১,১১,১১১


//Indian English

var number = new Number(1111111).toLocaleString("en-IN");
console.log(number); //11,11,111

Checkout the full list

toPrecision(x)    

Formats a number to x length

A decimal point and nulls are added (if needed), to create the specified length.

Examples


var number = 12.345678;
console.log(number.toPrecision(2));

var number = 1;
console.log(number.toPrecision(2));

//12
//1.0

toString()

Converts a number to a string

Examples


var number = 12.345678;
console.log(number.toString());

var number = 1;
console.log(number.toString());

//12.345678
//1

valueOf()  

Returns the primitive value of a number.

The valueOf() method is used internally in JavaScript to convert Number objects to primitive values.

All JavaScript data types have a valueOf() and a toString() method.

Examples


var number = 12.345678;
console.log(number.valueOf());

var number = 1;
console.log(number.valueOf());

//12.345678
//1

Sources


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
A Quick Comparison of JOIN and Subquery in SQLSQL/MySQLRevisiting variable scope in JavaScriptJavaScriptFETCH clause in PostgreSQLPostgresVariables and Distance Functions in SCSS (Sass)SCSSfor @each loop in SCSSSCSSHow to compile and watch Sass using Gulp in WordPress?SCSS