JavaScript Number Methods.

Working with numbers is an integral part of programming. The JavaScript number object is a primitive wrapper object used to represent and manipulate numbers.

1. parseInt() Method

The parseInt() method parses the given string argument and returns an integer number parsed from the string.

let num1 = Number.parseInt("34");
console.log(num1);

let num2 = Number.parseInt("5324 ");
console.log(num2);

let num3 = Number.parseInt("32.65");
console.log(num3);

Output:

34
5324
32

If an integer can’t be parsed from the given string, the method returns NaN.

let num4 = Number.parseInt("Hello, World!");
console.log(num4);

let num5 = Number.parseInt("...#@$$");
console.log(num5);

Output:

NaN
NaN

2. toString() Method

The toString() method returns the given number in the form of a string. This method accepts radix (the base in mathematical numeral systems) as an optional parameter and returns a string representing the specified Number object.

let num1 = 213;
console.log(num1.toString());

let num2 = 25.56;
console.log(num2.toString());

let num3 = -673;
console.log(num3.toString());

let num4 = 15;
// Base 2
console.log(num4.toString(2));

Output:

213
25.56
-673
1111

3. toExponential() Method

The toExponential() method returns a string that represents the exponential notation of the given number. This method accepts fractionDigits as an optional parameter that specifies the number of digits after the decimal point.

let num1 = 23425;
console.log(num1.toExponential());

let num2 = 342;
console.log(num2.toExponential(2));

let num3 = 465500;
console.log(num3.toExponential(4));

let num4 = 886.456;
console.log(num4.toExponential());

let num5 = 0.34;
console.log(num5.toExponential());

Output:

2.3425e+4
3.42e+2
4.6550e+5
8.86456e+2
3.4e-1

4. toFixed() Method

The toFixed() method returns a string that represents a number formatted using fixed-point notation. This method accepts an optional parameter that specifies the number of digits to appear after the decimal point. If no parameter is provided, the value of this parameter is treated as 0.

let num1 = 234.345;
console.log(num1.toFixed(1));

let num2 = -783.234;
console.log(num2.toFixed(2));

let num3 = 213;
console.log(num3.toFixed(4));

let num4 = 345.23;
console.log(num4.toFixed());

let num5 = 785.123;
console.log(num5.toFixed(0));

Output:

234.3
-783.23
213.0000
345
785

5. toPrecision() Method

The toPrecision() method returns a string representing the number to the specified precision. This method accepts an optional parameter that specifies the number of significant digits.

let num1 = 234.345;
console.log(num1.toPrecision(4));

let num2 = -783.234;
console.log(num2.toPrecision(5));

let num3 = 213;
console.log(num3.toPrecision(4));

let num4 = 345.23;
console.log(num4.toPrecision(3));

let num5 = 785.123;
console.log(num5.toPrecision(5));

Output:

234.3
-783.23
213.0
345
785.12

6. valueOf() Method

The valueOf() method returns the primitive value of a Number object.

let num1 = 234.345;
console.log(num1.valueOf());

let num2 = -783.234;
console.log(num2.valueOf());

console.log((327).valueOf());

console.log((25+25).valueOf());

console.log((0.003).valueOf());

Output:

234.345
-783.234
327
50
0.003

7. toLocaleString() Method

The JavaScript toLocaleString() method returns a string with a language-sensitive representation of a number.

let num = 762359.237;

// Indian
console.log(num.toLocaleString('en-IN'));

// Chinese
console.log(num.toLocaleString('zh-Hans-CN-u-nu-hanidec'));

// German
console.log(num.toLocaleString('de-DE'));

Output:

7,62,359.237
七六二,三五九.二三七
762.359,237

8. parseFloat() Method

The parseInt() method parses the given string argument and returns a floating-point number parsed from the string.

let num1 = Number.parseFloat("34.235");
console.log(num1);

let num2 = Number.parseFloat(" 5324.45 ");
console.log(num2);

let num3 = Number.parseFloat("32.65");
console.log(num3);

let num4 = Number.parseFloat("2 Welcome MUO");
console.log(num4);

Output:

34.235
5324.45
32.65
2

If a number can’t be parsed from the given string, the method returns NaN.

let num5 = Number.parseFloat("Welcome 2 MUO"); 
console.log(num5);
let num6 = Number.parseFloat("#$^$^");
console.log(num6);

Output:

NaN
NaN

9. isInteger() Method

The isInteger() method checks whether the passed value is an integer. This method returns a Boolean value (true or false) that indicates whether the given value is an integer or not.

let num1 = 45;
console.log(Number.isInteger(num1));

let num2 = 0;
console.log(Number.isInteger(num2));

let num3 = 1;
console.log(Number.isInteger(num3));

let num4 = 0.8;
console.log(Number.isInteger(num4));

let num5 = 8.0;
console.log(Number.isInteger(num5));

let num6 = Infinity;
console.log(Number.isInteger(num6));

let num7 = NaN;
console.log(Number.isInteger(num7));

let num8 = [1, 2, 3];
console.log(Number.isInteger(num8));

let num9 = "45";
console.log(Number.isInteger(num9));

let num10 = true;
console.log(Number.isInteger(num10));

Output:

true
true
true
false
true
false
false
false
false
false

10. isFinite() Method

The isFinite() method checks whether the passed value is a finite number. This method returns a Boolean value (true or false) that indicates whether the given value is finite or not.

let num1 = 386483265486;
console.log(Number.isFinite(num1));

let num2 = 0000000;
console.log(Number.isFinite(num2));

let num3 = Infinity;
console.log(Number.isFinite(num3));

let num4 = -Infinity;
console.log(Number.isFinite(num4));

let num5 = 32e34;
console.log(Number.isFinite(num5));

let num6 = '0';
console.log(Number.isFinite(num6));

let num7 = NaN;
console.log(Number.isFinite(num7));

let num8 = 0 / 0;
console.log(Number.isFinite(num8));

let num9 = null;
console.log(Number.isFinite(num9));

let num10 = 23/0;
console.log(Number.isFinite(num10));

Output:

true
true
false
false
true
false
false
false
false
false

11. isSafeInteger() Method

The isSafeInteger() method checks whether a value is a safe integer. This method returns a Boolean value (true or false) that indicates whether the given value is a safe integer or not.

According to the official MDN Docs, a safe integer is an integer that:

  • can be exactly represented as an IEEE-754 double-precision number, and
  • whose IEEE-754 representation cannot be the result of rounding any other integer to fit the IEEE-754 representation.
let num1 = 386483265486;
console.log(Number.isSafeInteger(num1));

let num2 = 0000000;
console.log(Number.isSafeInteger(num2));

let num3 = Infinity;
console.log(Number.isSafeInteger(num3));

let num4 = -Infinity;
console.log(Number.isSafeInteger(num4));

let num5 = 32e34;
console.log(Number.isSafeInteger(num5));

let num6 = '0';
console.log(Number.isSafeInteger(num6));

let num7 = NaN;
console.log(Number.isSafeInteger(num7));

let num8 = 34;
console.log(Number.isSafeInteger(num8));

let num9 = null;
console.log(Number.isSafeInteger(num9));

let num10 = 45.67;
console.log(Number.isSafeInteger(num10));

Output:

true
true
false
false
true
false
false
false
false
false

12. isNaN() Method

The isNaN() method checks whether a value is a NaN and its type is Number. This method returns true if the given value is NaN and its type is Number, otherwise, it returns false.

let num1 = NaN;
console.log(Number.isNaN(num1));

let num2 = "NaN";
console.log(Number.isNaN(num2));

let num3 = Infinity;
console.log(Number.isNaN(num3));

let num4 = "string"/5;
console.log(Number.isNaN(num4));

let num5 = 32e34;
console.log(Number.isNaN(num5));

let num6 = '0';
console.log(Number.isNaN(num6));

let num7 = undefined;
console.log(Number.isNaN(num7));

let num8 = {};
console.log(Number.isNaN(num8));

Output:

true
false
false
true
false
false
false
false

Related Posts

What are custom events in JavaScript?

Custom events are the events that allow you to decouple the code you want to run after a specific piece of code runs. There are various in-built events…

How to use nested for loop in JavaScript?

We use the for loop statement of JavaScript for repeating a set of statements inside the loop body a specified number of times. A nested for loop, as the…

What are the basic rules for JavaScript parameters?

A JavaScript function is a code that performs a particular task. The function parameters are the name list in the function definition. Parameters are also known as…

How to stop refreshing the page on submit in JavaScript?

Using event.preventDefault() to stop page refresh on form submit In this section, we will see how to use event.preventDefault() to stop page refresh on form submission. The event.preventDefault() restricts the default…

Target a Window Using JavaScript or HTML

TARGET attribute of HTML A link’s opening named frame or window is specified using the Target attribute of the <a> anchor tag. The concluding </a> tag in…

What is the role of deferred scripts in JavaScript?

Since JavaScript is a loosely typed language, you are not required to correctly predict the kind of data that will be kept in a variable. Depending on the information…