温馨提示:本站仅提供公开网络链接索引服务,不存储、不篡改任何第三方内容,所有内容版权归原作者所有
AI智能索引来源:http://www.muo.com/javascript-number-methods/
点击访问原文链接

12 JavaScript Number Methods You Should Know

12 JavaScript Number Methods You Should Know Menu Sign in now Close PC & Mobile Submenu Windows2 Linux Android Apple Technical Submenu Tech Explained Security Software Submenu Productivity Internet Creative Screen Submenu Entertainment Streaming Home Submenu Smart Home Home News Sign in Newsletter Menu Follow Followed Like More Action Sign in now Productivity Android Smart TVs Networking Windows 11 Entertainment Close
12 JavaScript Number Methods You Should Know Background - Pexels Logos - Uxwing By  Yuvraj Chandra Published Dec 9, 2021, 9:00 AM EST

Yuvraj is a passionate technical writer with a computer science degree from the esteemed University of Delhi, India.

His deep understanding and expertise in programming, software development, artificial intelligence, and blockchain have driven his passion for writing on cutting-edge technology. Since 2019, he has been a technical writer, sharing his knowledge on various web development technologies. Yuvraj's stint as a developer for several startups complements his writing skills.

In addition to his professional pursuits, Yuvraj enjoys playing chess and has contributed to prominent publications, including GeeksforGeeks

Sign in to your MakeUseOf account

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

In this article, you'll learn 12 JavaScript Number methods that you should know.

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

Related: JavaScript One-Liners You Should Know

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

Related: JavaScript Set Methods You Should Master Today

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

Related: JavaScript Map Methods You Should Master Today

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

If you want to have a look at the complete source code used in this article, check out the GitHub repository.

Get Your JavaScript Basics Strong

JavaScript is one of the most popular programming languages used by web devs today. To develop amazing JavaScript-based projects, you first need to understand the fundamentals of the language. Get your hands dirty and solidify your JavaScript fundamentals.

Close
Recommended An Introduction to Data Types in JavaScript I repurposed 4 old Apple devices, and now they all do something useful in my home Claude just released a Mythos-level model, but you only have 10 days to try it Siri’s big AI upgrade is actually just Google in a trench coat Join Our Team Our Audience About Us Press & Events Media Coverage Contact Us Follow Us Advertising Careers Terms Privacy Policies MakeUseOf is part of the Valnet Publishing Group Copyright © 2026 Valnet Inc.

12 JavaScript Number Methods You Should Know,AI智能索引,全网链接索引,智能导航,网页索引

    Get a few steps closer to JavaScript mastery by learning and applying these number methods.