7 Most commonly used Number Methods in JavaScript

Number(value) – Returns the number from the provided value as an argument. If a boolean value is provided it will show 1 for true and 0 for false. But for a string with alphanumeric characters, it will return as NaN

Number("James"); //returns NaN
Number("25"); //returns 25
Number(true); //returns 1

toString() – Returns the string for the provided number.

(20).toString(); //returns "20"
(10*2).toString(); //returns "20"

Number.IsNaN() – Returns boolean value. If the value is not a number then returns true or if value provided is a number, it returns false.

var i = 34;
Number.isNaN(i); //returns false
var  j = "34 a";
Number.isNaN(j); //return false

Number.isInteger(value) – Returns boolean value based on whether provided value is integer or not.

var i = 34;
Number.isInteger(i); //returns true
var  j = 35.25;
Number.isInteger(j); //returns false

toFixed([digits]) – Returns the string representing the provided number by using specified number of decimals as an argument.

var i = 1.757455;
i.toFixed(); //Returns "2"
i.toFixed(2); //Returns "1.76"
i.Fixed(4); //Returns "1.7575"

parseInt(value) – Returns a whole number from the provided value. If space is provided it will try to convert only string provided before the space.

parseInt(27.27); //returns 27
parseInt("hello 1"); //returns NaN
parseInt("1 hello"); //returns 1
parseInt(2); //returns 2

parseFloat(value) – Returns a number by parsing the provided value. If space is provided it will try to convert only string provided before the space.

parseFloat("27.27"); //returns 27.27
parseFloat("hello 1"); //returns NaN
parseFloat("1 hello"); //returns 1
parseFloat(2); //returns 2

I hope this post was helpful to you. If you like my post, follow me @twitter/andramazo.

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top