DEV Community

김영민
김영민

Posted on

Mastering JavaScript Numbers

JavaScript Numbers

JavaScript numbers are a fundamental data type, playing a crucial role in various calculations and operations. In this article, we'll delve into the key concepts related to JavaScript numbers.

1. Number Data Type

In JavaScript, numbers are defined as the number type. Unlike some other programming languages, JavaScript does not distinguish between integers and floating-point numbers; both are treated as number types.

let integer = 42;   // integer
let float = 3.14;    // floating-point
Enter fullscreen mode Exit fullscreen mode

2. Number Literals

There are four ways to represent numbers in JavaScript:

  • Decimal: The default representation is decimal.
  • Binary: Numbers starting with 0b or 0B are binary.
  • Octal: Numbers starting with 0o or 0O are octal.
  • Hexadecimal: Numbers starting with 0x or 0X are hexadecimal.
let decimal = 123;   // decimal
let binary = 0b111;   // binary (7)
let octal = 0o17;     // octal (15)
let hexadecimal = 0x1F; // hexadecimal (31)
Enter fullscreen mode Exit fullscreen mode

3. Number Range

JavaScript numbers are represented in a 64-bit floating-point format, following the IEEE 754 standard. This allows JavaScript to represent both very large numbers and very small fractions, but there are limits to the precision.

Maximum and Minimum Values

  • Number.MAX_VALUE: The largest number that can be represented in JavaScript.
  • Number.MIN_VALUE: The smallest positive number that can be represented in JavaScript (closest to zero).

Example

console.log(Number.MAX_VALUE);  // 1.7976931348623157e+308
console.log(Number.MIN_VALUE);  // 5e-324
Enter fullscreen mode Exit fullscreen mode

4. Special Number Values

JavaScript can handle special number values:

  • Infinity: Positive infinity. For example, dividing 1 by 0 returns Infinity.

    let infiniteValue = 1 / 0;
    console.log(infiniteValue);  // Infinity
    
    
  • -Infinity: Negative infinity.

    let negativeInfiniteValue = -1 / 0;
    console.log(negativeInfiniteValue);  // -Infinity
    
    
  • NaN: "Not a Number". Represents a value that is not a number. For example, trying to divide a string by a number.

    let notANumber = "hello" / 2;
    console.log(notANumber);  // NaN
    
    

5. Comparing NaN

NaN is unique in that it cannot be compared to itself. Therefore, comparing NaN to NaN returns false. To compare NaN, use Number.isNaN().

let a = NaN;
console.log(a === NaN);  // false
console.log(Number.isNaN(a));  // true
Enter fullscreen mode Exit fullscreen mode

6. Floating-Point Operations and Precision Issues

Since JavaScript uses floating-point numbers, precision issues can arise during calculations.

Example

console.log(0.1 + 0.2);  // 0.30000000000000004
Enter fullscreen mode Exit fullscreen mode

To resolve this, you can adjust the precision. One way is to use toFixed().

let result = (0.1 + 0.2).toFixed(2);  // 0.30
console.log(result);  // "0.30"
Enter fullscreen mode Exit fullscreen mode

7. Number Methods

1. Number() Function

The Number() function converts a value to a number.

console.log(Number("123"));  // 123
console.log(Number("abc"));  // NaN
Enter fullscreen mode Exit fullscreen mode

2. parseInt() and parseFloat()

  • parseInt() converts a string to an integer.
  • parseFloat() converts a string to a floating-point number.
console.log(parseInt("123abc"));    // 123
console.log(parseFloat("12.34abc")); // 12.34
Enter fullscreen mode Exit fullscreen mode

3. toFixed()

toFixed() returns a string representation of a number, rounded to a specified number of decimal places.

let num = 123.456;
console.log(num.toFixed(2));  // "123.46"
Enter fullscreen mode Exit fullscreen mode

4. toPrecision()

toPrecision() returns a string representation of a number, rounded to a specified number of significant digits.

let num = 123.456;
console.log(num.toPrecision(4));  // "123.5"
Enter fullscreen mode Exit fullscreen mode

8. BigInt

BigInt is a new data type in JavaScript that allows you to work with very large integers, beyond the range of Number.

let bigIntValue = 1234567890123456789012345678901234567890n;
console.log(bigIntValue);  // 1234567890123456789012345678901234567890n
Enter fullscreen mode Exit fullscreen mode

9. Arithmetic Operators

  • Addition (+), subtraction (-), multiplication (*), division (/)
  • Modulus (%), exponentiation (**)
let x = 10;
let y = 3;

console.log(x + y);  // 13
console.log(x - y);  // 7
console.log(x * y);  // 30
console.log(x / y);  // 3.333...
console.log(x % y);  // 1
console.log(x ** y); // 1000
Enter fullscreen mode Exit fullscreen mode

10. Characteristics of Numbers

  • Numbers in JavaScript are immutable. You cannot change a number; you must assign a new value.
  • Thanks to JavaScript's dynamic typing, numbers can be combined with values of other types. For example, when combined with strings, numbers are automatically converted.

In conclusion, understanding how JavaScript handles numbers is essential for any developer. From the basics of number literals and data types to more advanced topics like precision issues and special number values, mastering numbers in JavaScript will make you a more proficient and effective developer. By following the tips and examples provided in this article, you'll be well on your way to becoming a JavaScript expert.

Top comments (0)