YogeshChauhan.com

JavaScript Number Methods

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

dreamhost

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 uninstall Cocoapods from the Mac OS? #4 How to Use SQL MAX() Function with Dates? #5 How to add Read More Read Less Button using JavaScript? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Apr 7 Solution for “Yarn build: Failed because of a stylelint error” Apr 7 Make sure your links have descriptive text Apr 7 How to add Laravel to WordPress using Sage theme (and install Tailwind CSS)? Apr 2 How to create a sidebar using pure CSS? Apr 1 How to switch dark and light themes using pure CSS? Apr 1 How to calculate elapsed time in JavaScript?

You might also like these

How to create a smooth scrolling effect with CSS?CSSHow to change HTML content on click using JavaScript?JavaScriptEssential SQL Commands We Need to KnowSQL/MySQLHow to Use password_hash and password_verify to Secure Your User’s Data (Especially Passwords)?PHPKilling A Project Part 3: How can an organization ensure that a doomed project is killed as early as possible?MiscHow to embed YouTube or other video links in WordPress?WordPress