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 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
JavaScript arrays: a separate data type or Objects?JavaScriptHow to add transparent text on top of an image using CSS?CSSHow to Commit and Rollback Changes in SQL?SQL/MySQL@use rule in SCSS (Sass)SCSSDid you get an error while displaying Special Characters in JavaScript?JavaScriptSolution to Could not cast value of type ‘NSTaggedPointerString’ to ‘NSNumber’Swift