DEV Community

Cover image for What are number methods and how do they impact performance?
The daily developer
The daily developer

Posted on

What are number methods and how do they impact performance?

Number methods are built-in methods that intend to make working with numbers easier and more effective by handling operations, conversions and checks.

Overview:

  1. Number methods
  2. Number properties
  3. How number methods affect performance.

Number Methods

Number methods, allow you to manipulate numbers in various ways without the need to write custom code.

Method Description
toFixed(digits) Formats a number as a string with fixed decimals
toString() Converts a number to a string
parseInt(str) Parses a string to an integer
parseFloat(str) Parses a string to a decimal number
isNaN(value) Checks if a value is NaN (Not-a-Number)
isFinite(value) Checks if a value is a finite number
toExponential(digits) Formats a number in exponential notation
toPrecision(precision) Formats a number with a specified precision
  • toFixed()
let num = 5.324578;
let formattedNum = num.toFixed(2); // "5.32"
Enter fullscreen mode Exit fullscreen mode
  • toString()
let num = 15;
let str = num.toString(); // "15" 
Enter fullscreen mode Exit fullscreen mode
  • parseInt(str)
let str = "543";
let newInt = parseInt(str); // 543
Enter fullscreen mode Exit fullscreen mode
  • parseFloat()
let floatStr = "1.23";
let parsedFloat = parseFloat(floatStr); // 1.23
Enter fullscreen mode Exit fullscreen mode
  • isNaN() The isNaN() method checks if the value passed to it can be converted to a number or not. It returns a boolean value true or false. if the value can be converted to a number it returns false, otherwise it returns true.
isNaN("hello");  // true 
isNaN(26);       // false
Enter fullscreen mode Exit fullscreen mode
  • isFinite()
let finiteValue = 42;
let infiniteValue = Infinity;
const finiteCheck = isFinite(finiteValue);     // true
const infiniteCheck = isFinite(infiniteValue); // false
Enter fullscreen mode Exit fullscreen mode

In this example we've used the Infinity keyword which is a special numeric value that demonstrates positive infinity. That is why the infinityCheck returned false

  • toExponential()

Exponential notation are a form of notation where very large or very small numbers can be expressed in a smaller and practical form. For example

2000 = 2 x 10^3
0.0012 = 1.2 x 10^-3
Enter fullscreen mode Exit fullscreen mode

The toExponential() method formats number into exponential notation with the specified number of decimal as argument.

let num = 54321.9876;
let expNum = num.toExponential(2); // 5.43e+4
Enter fullscreen mode Exit fullscreen mode
  • toPrecision() This method formats numbers with a predetermined total number of significant digits.
let num = 226.456;
let preciseNum = num.toPrecision(5); //226.46
Enter fullscreen mode Exit fullscreen mode

In this example we've passed 5 as argument which tells the method to return a number of 5 digits.

Number properties

Number properties give important information and constants related to numeric values. they provide explanations for the properties and limitations of numbers.

Property Description
Number.MAX_VALUE The largest representable number in JavaScript
Number.MIN_VALUE The smallest positive representable number in JavaScript (close to zero)
Number.POSITIVE_INFINITY Represents positive infinity (e.g., 1/0)
Number.NEGATIVE_INFINITY Represents negative infinity (e.g., -1/0)
Number.NaN Represents "Not-a-Number"
Number.EPSILON The smallest difference between two representable numbers
Number.MAX_SAFE_INTEGER The largest integer that can be safely represented without loss of precision
Number.MIN_SAFE_INTEGER The smallest integer that can be safely represented without loss of precision
  • MAX_VALUE is 1.7976931348623157e+308
  • MIN_VALUE is 5e-324
  • POSITIVE_INFINITY is Infinity
  • NEGATIVE_INFINITY is -Infinity
  • NaN is NaN (not a number)
  • EPSILON is 2.220446049250313e-16
  • MAX_SAFE_INTEGER is 9007199254740991
  • MIN_SAFE_INTEGER is -9007199254740991

How number methods affect performance

Performance in JavaScript is significantly impacted by the use of number methods, particularly when performing calculations, data processing, and algorithmic operations. These techniques offer a number of advantages, such as quick operations that produce code that is cleaner and more concise, the maintenance of accuracy and precision with techniques like toFixed(). To benefit from these advantages, become familiar with number methods, but test your code first to ensure that it satisfies your performance needs.

Along this series we'll see more methods such as the Math Object methods that will allow us to perform many mathematical operations.

Top comments (0)