Working with numbers is an integral part of programming. A JavaScript number object is a raw shell object used to represent and manipulate numbers. JavaScript provides several methods for working with numbers.
In this article, you will learn 12 JavaScript Numbering Methods that you should know.
1. parseInt() method
The parseInt() The method parses the given string argument and returns an integer 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);
production |:
34
5324
32
If an integer of the given string cannot be parsed, the method returns n.
let num4 = Number.parseInt("Hello, World!");
console.log(num4);
let num5 = Number.parseInt("...#@$$");
console.log(num5);
production |:
NaN
NaN
2. toString() method
The to string() Method for returning the specified number as a string. This method accepts the root (the base in mathematical number systems) as an optional parameter and returns a string representing the given 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));
production |:
213
25.56
-673
1111
3. method toExponential .() method
The to relative() The method returns a string representing 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());
production |:
2.3425e+4
3.42e+2
4.6550e+5
8.86456e+2
3.4e-1
4. Method toFixed
The to repair() The method returns a string representing a number formatted using fixed point notation. This method accepts an optional parameter that specifies the number of digits that appear after the decimal point. If no parameter is provided, the value of that 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));
production |:
234.3
-783.23
213.0000
345
785
5. Precision() method
The to res() 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));
production |:
234.3
-783.23
213.0
345
785.12
6. valueOf() method
The The value of the() The method returns the initial 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());
production |:
234.345
-783.234
327
50
0.003
7. toLocaleString() method
JavaScript toLocaleString() The 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'));
production |:
7,62,359.237
七六二,三五九.二三七
762.359,237
8. parseFloat() method
The parseInt() The method parses the argument of the given string and returns the parsed floating point number 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);
production |:
34.235
5324.45
32.65
2
If a number cannot be parsed from the given string, the method returns n.
let num5 = Number.parseFloat("Welcome 2 MUO");
console.log(num5);
let num6 = Number.parseFloat("#$^$^");
console.log(num6);
production |:
NaN
NaN
9. isInteger() method
The isInteger() The method checks if the value passed is an integer. This method returns a Boolean value (real or False) indicates whether the value provided 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));
production |:
true
true
true
false
true
false
false
false
false
false
10. isFinite() method
The isFinite() The method checks if the value passed is a given number. This method returns a Boolean value (real or False) indicates whether the specified 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));
production |:
true
true
false
false
true
false
false
false
false
false
11. isSafeInteger() method
The isSafeInteger() The method checks if the value is a safe integer. This method returns a Boolean value (real or False) indicates whether the given value is a safe integer.
According to the official MDN Documents, a safe integer is an integer:
- It can be represented exactly as a double-precision IEEE-754 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));
production |:
true
true
false
false
true
false
false
false
false
false
12. isNaN() method
The isNaN() The method checks if the value a n Its type is a number. This method is back real 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));
production |:
true
false
false
true
false
false
false
false
If you want to take a look at the full source code used in this article, check out the . file GitHub repository.
Get strong JavaScript basics
JavaScript is one of the most popular programming languages used by web developers today. To develop amazing JavaScript-based projects, you first need to understand the basics of the language. Get your hands dirty and solidify your JavaScript basics.
read the following
About the author