Sling Academy
Home/JavaScript/Working with Numeric Types and Values in JavaScript

Working with Numeric Types and Values in JavaScript

Last updated: December 12, 2024

JavaScript is a versatile programming language that allows you to work with various data types, including numbers. Numbers in JavaScript are divided into several types, such as integers, floating points, and special numeric values like NaN, Infinity, and -Infinity. Understanding these numeric types and their behaviors is crucial for effective JavaScript programming.

Basic Numbers

In JavaScript, all numbers are stored in a 64-bit floating-point format, which is based on the IEEE 754 standard. This means both integers and floating-point numbers are technically stored with the same datatype.

// Basic numeric declaration
let integerNum = 100;
let floatNum = 100.50;

Numeric Values

Numbers in JavaScript can also represent special values:

  • NaN (Not-a-Number): This special value is returned when a mathematical operation does not yield a meaningful result. For example, dividing zero by zero returns NaN.
  • Infinity and -Infinity: These represent positive and negative infinity, results of calculations like dividing a positive number by zero and a shift in the numeric bounds.
// Examples of special numeric values
let notANumber = 0 / 0;  // Returns NaN
let positiveInfinity = 1 / 0;  // Returns Infinity
let negativeInfinity = -1 / 0;  // Returns -Infinity

BigInt

To handle extremely large integers, JavaScript introduced BigInt in ES2020. This numeric type allows for arbitrarily large integer values, beyond the safe integer limit of Number.MAX_SAFE_INTEGER, which is 253 - 1.

let bigIntValue = 1234567890123456789012345678901234567890n;
let anotherBigInt = BigInt(123456789012345678901234567890);

Number Methods

JavaScript provides several useful methods for operations on numeric values:

  • Number.isInteger(value): This method checks if the value is an integer.
  • Number.isNaN(value): Determines whether the supplied value is NaN.
  • Number.isFinite(value): Verifies if the value is a finite number, excluding Infinity, -Infinity, and NaN.
  • parseFloat(string) and parseInt(string): These methods convert strings into numbers.
let num = 150;

console.log(Number.isInteger(num));           // true
console.log(Number.isNaN(notANumber));        // true
console.log(Number.isFinite(positiveInfinity)); // false
console.log(parseInt("42"));                  // 42
console.log(parseFloat("3.14"));               // 3.14

Precision and Rounding

JavaScript uses floating-point arithmetic, which can introduce precision errors during calculations. For instance:

// Precision issue
console.log(0.1 + 0.2 === 0.3); // false
console.log(0.1 + 0.2);         // 0.30000000000000004

Such issues can be mitigated using methods like toFixed() or by using external libraries for precise calculations.

let preciseNum = (0.1 + 0.2).toFixed(2);
console.log(preciseNum);  // "0.30"

Conclusion

Working with numbers in JavaScript can be straightforward once you understand the types and operations available. From handling simple integers to complex calculations with BigInt and managing potential pitfalls like floating-point precision, JavaScript provides a robust suite of tools for numeric manipulation.

Next Article: Refining Numerical Operations Using JavaScript Math Methods

Previous Article: JavaScript BigInt: Tutorial & Examples

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