Sling Academy
Home/JavaScript/Comparing Numbers Reliably in JavaScript

Comparing Numbers Reliably in JavaScript

Last updated: December 12, 2024

In JavaScript, comparing numbers is a core operation that you may encounter frequently as a programmer. Although it seems straightforward, there are some intricacies involved when comparing numbers, particularly when dealing with floating-point precision and special numeric values. This article aims to cover all the necessary aspects of comparing numbers reliably in JavaScript, presenting clear examples to illustrate each concept.

Basic Comparison Operators

JavaScript provides a variety of operators to compare numbers:

  • == (Equality)
  • === (Strict Equality)
  • != (Inequality)
  • !== (Strict Inequality)
  • > (Greater than)
  • >= (Greater than or equal to)
  • < (Less than)
  • <= (Less than or equal to)

Here's how you can use these operators:

let a = 5;
let b = 10;

console.log(a < b);   // Output: true
console.log(a >= b);  // Output: false
console.log(a === 5);  // Output: true
console.log(a !== 5);  // Output: false

Floating-Point Precision Issues

Floating-point arithmetic can lead to precision issues because numbers are represented in binary. For example, you might expect 0.1 + 0.2 to be 0.3, but:

console.log(0.1 + 0.2 === 0.3);  // Output: false

This happens due to the way floating-point numbers are stored. To handle such cases, it's often easier to use a small threshold to check equality:

function areEqualWithPrecision(num1, num2, precision) {
    return Math.abs(num1 - num2) < precision;
}

console.log(areEqualWithPrecision(0.1 + 0.2, 0.3, 0.0001));  // Output: true

Comparing Special Numeric Values

JavaScript has some special values like NaN, Infinity, and -Infinity, which can behave surprisingly if not understood well:

console.log(NaN === NaN);        // Output: false
console.log(Infinity > 1000);    // Output: true
console.log(-Infinity < 1000);   // Output: true

For NaN, you should use the isNaN() function:

let x = 0 / 0;
console.log(isNaN(x));  // Output: true

Strict vs. Loose Comparison

The == operator performs type conversion before comparison, which can lead to unexpected results:

console.log(1 == '1');    // Output: true
console.log(1 === '1');   // Output: false

It's generally safer to use === to avoid issues that arise from type coercion.

Handling BigInt

For dealing with very large integers, JavaScript supports BigInt:

let bigInt1 = 1234567890123456789012345678901234567890n;
let bigInt2 = 1234567890123456789012345678901234567890n;

console.log(bigInt1 === bigInt2); // Output: true

Note that BigInt comparisons using === work as expected, but mixing BigInt with regular numbers in operations requires using same data types to avoid TypeError.

Conclusion

In JavaScript, while comparing numbers might initially seem straightforward, special considerations like floating-point precision or handling special numeric values warrant attention. Developers should strive to ensure precision and reliability when comparing numbers through appropriate strategies and utility functions. By doing so, you can avoid many common pitfalls that could lead to subtle bugs in your applications.

Next Article: Leveraging toFixed(), toPrecision(), and toExponential() in JavaScript

Previous Article: Avoiding Floating-Point Pitfalls in JavaScript Calculations

Series: JavaScript Numbers

JavaScript

You May Also Like

  • Handle Zoom and Scroll with the Visual Viewport API in JavaScript
  • Improve Security Posture Using JavaScript Trusted Types
  • Allow Seamless Device Switching Using JavaScript Remote Playback
  • Update Content Proactively with the JavaScript Push API
  • Simplify Tooltip and Dropdown Creation via JavaScript Popover API
  • Improve User Experience Through Performance Metrics in JavaScript
  • Coordinate Workers Using Channel Messaging in JavaScript
  • Exchange Data Between Iframes Using Channel Messaging in JavaScript
  • Manipulating Time Zones in JavaScript Without Libraries
  • Solving Simple Algebraic Equations Using JavaScript Math Functions
  • Emulating Traditional OOP Constructs with JavaScript Classes
  • Smoothing Out User Flows: Focus Management Techniques in JavaScript
  • Creating Dynamic Timers and Counters with JavaScript
  • Implement Old-School Data Fetching Using JavaScript XMLHttpRequest
  • Load Dynamic Content Without Reloading via XMLHttpRequest in JavaScript
  • Manage Error Handling and Timeouts Using XMLHttpRequest in JavaScript
  • Handle XML and JSON Responses via JavaScript XMLHttpRequest
  • Make AJAX Requests with XMLHttpRequest in JavaScript
  • Customize Subtitle Styling Using JavaScript WebVTT Integration