DEV Community

Abhay Yt
Abhay Yt

Posted on

Mastering Number Methods in JavaScript

Number Methods in JavaScript

JavaScript provides several built-in methods to work with numbers effectively. These methods allow you to perform operations like formatting, rounding, parsing, and validating numbers.


1. Converting Numbers

A. toString()

Converts a number to a string.

const num = 123;
console.log(num.toString()); // Output: "123"
console.log((456).toString()); // Output: "456"
Enter fullscreen mode Exit fullscreen mode

B. toFixed(digits)

Formats a number with a fixed number of decimal places.

const num = 3.14159;
console.log(num.toFixed(2)); // Output: "3.14"
console.log(num.toFixed(4)); // Output: "3.1416"
Enter fullscreen mode Exit fullscreen mode

C. toExponential(digits)

Returns the number in exponential notation.

const num = 12345;
console.log(num.toExponential(2)); // Output: "1.23e+4"
Enter fullscreen mode Exit fullscreen mode

D. toPrecision(digits)

Formats a number to a specified total length (including decimals).

const num = 123.456;
console.log(num.toPrecision(4)); // Output: "123.5"
console.log(num.toPrecision(6)); // Output: "123.456"
Enter fullscreen mode Exit fullscreen mode

2. Parsing and Validating Numbers

A. parseInt(string, radix)

Parses a string into an integer.

console.log(parseInt("123")); // Output: 123
console.log(parseInt("101", 2)); // Output: 5 (binary to decimal)
Enter fullscreen mode Exit fullscreen mode

B. parseFloat(string)

Parses a string into a floating-point number.

console.log(parseFloat("3.14")); // Output: 3.14
console.log(parseFloat("123abc")); // Output: 123
Enter fullscreen mode Exit fullscreen mode

C. Number.isInteger(value)

Checks if a value is an integer.

console.log(Number.isInteger(123)); // Output: true
console.log(Number.isInteger(3.14)); // Output: false
Enter fullscreen mode Exit fullscreen mode

D. Number.isFinite(value)

Checks if a value is a finite number.

console.log(Number.isFinite(123)); // Output: true
console.log(Number.isFinite(Infinity)); // Output: false
Enter fullscreen mode Exit fullscreen mode

E. Number.isNaN(value)

Checks if a value is NaN (Not-a-Number).

console.log(Number.isNaN(NaN)); // Output: true
console.log(Number.isNaN(123)); // Output: false
Enter fullscreen mode Exit fullscreen mode

3. Rounding Numbers

A. Math.round()

Rounds a number to the nearest integer.

console.log(Math.round(4.5)); // Output: 5
console.log(Math.round(4.4)); // Output: 4
Enter fullscreen mode Exit fullscreen mode

B. Math.ceil()

Rounds a number up to the next integer.

console.log(Math.ceil(4.1)); // Output: 5
Enter fullscreen mode Exit fullscreen mode

C. Math.floor()

Rounds a number down to the previous integer.

console.log(Math.floor(4.9)); // Output: 4
Enter fullscreen mode Exit fullscreen mode

D. Math.trunc()

Removes the decimal part of a number.

console.log(Math.trunc(4.9)); // Output: 4
Enter fullscreen mode Exit fullscreen mode

4. Generating Random Numbers

A. Math.random()

Generates a random number between 0 (inclusive) and 1 (exclusive).

console.log(Math.random()); // Output: A random number between 0 and 1
Enter fullscreen mode Exit fullscreen mode

To generate a random number within a range:

const min = 1;
const max = 10;
const random = Math.floor(Math.random() * (max - min + 1)) + min;
console.log(random); // Output: A random number between 1 and 10
Enter fullscreen mode Exit fullscreen mode

5. Other Useful Number Methods

A. Math.abs()

Returns the absolute value of a number.

console.log(Math.abs(-5)); // Output: 5
Enter fullscreen mode Exit fullscreen mode

B. Math.pow(base, exponent)

Returns the base raised to the power of the exponent.

console.log(Math.pow(2, 3)); // Output: 8
Enter fullscreen mode Exit fullscreen mode

C. Math.sqrt()

Returns the square root of a number.

console.log(Math.sqrt(16)); // Output: 4
Enter fullscreen mode Exit fullscreen mode

D. Math.max() and Math.min()

Finds the maximum or minimum value among a set of numbers.

console.log(Math.max(1, 5, 3)); // Output: 5
console.log(Math.min(1, 5, 3)); // Output: 1
Enter fullscreen mode Exit fullscreen mode

E. Math.sign()

Returns the sign of a number (-1, 0, or 1).

console.log(Math.sign(-10)); // Output: -1
console.log(Math.sign(0));   // Output: 0
console.log(Math.sign(10));  // Output: 1
Enter fullscreen mode Exit fullscreen mode

6. Summary

  • JavaScript provides a wide range of methods to work with numbers, from conversion to complex mathematical operations.
  • The Math object contains utility functions for advanced calculations.
  • Use these methods to handle rounding, parsing, formatting, and generating numbers efficiently.

By mastering these number methods, you can handle various numerical operations in JavaScript with ease.

Hi, I'm Abhay Singh Kathayat!
I am a full-stack developer with expertise in both front-end and back-end technologies. I work with a variety of programming languages and frameworks to build efficient, scalable, and user-friendly applications.
Feel free to reach out to me at my business email: kaashshorts28@gmail.com.

Top comments (0)