JavaScript Numbers

In JavaScript, numbers are a data type that represent both integers and floating-point values. JavaScript numbers are always stored as double-precision floating-point numbers.

1. Number Types

  • Integers: Whole numbers, positive or negative.
    let intNum = 42;
    

    Try It Now

  • Floating-point Numbers: Numbers with decimals.
    let floatNum = 3.14;
    

    Try It Now

  • Exponential Notation: Large or small numbers can be represented using exponential notation.
    let expNum = 5e6;  // 5000000
    

    Try It Now

2. Number Properties

  • MAX_VALUE: Largest possible number in JavaScript.
    console.log(Number.MAX_VALUE);  // Output: 1.7976931348623157e+308
    

    Try It Now

  • MIN_VALUE: Smallest possible positive number in JavaScript.
    console.log(Number.MIN_VALUE);  // Output: 5e-324
    

    Try It Now

  • NaN: Represents a value that is “Not-a-Number”.
    let result = 0 / 0;
    console.log(result);  // Output: NaN
    

    Try It Now

  • Infinity: Represents infinity, which is greater than any number.
    console.log(1 / 0);  // Output: Infinity
    

    Try It Now

    -Infinity: Represents negative infinity.

    console.log(-1 / 0);  // Output: -Infinity
    

    Try It Now

3. Number Methods

  • toString(): Converts a number to a string.
    let num = 123;
    console.log(num.toString());  // Output: "123"
    

    Try It Now

  • toFixed(): Formats a number to a specified number of decimal places.
    let num = 5.6789;
    console.log(num.toFixed(2));  // Output: "5.68"
    

    Try It Now

  • toPrecision(): Formats a number to a specified length.
    let num = 5.6789;
    console.log(num.toPrecision(3));  // Output: "5.68"
    

    Try It Now

  • valueOf(): Returns the primitive value of a number.
    let num = new Number(123);
    console.log(num.valueOf());  // Output: 123
    

    Try It Now

4. Number Conversion

  • parseInt(): Parses a string and returns an integer.
    let num = parseInt('123.45');
    console.log(num);  // Output: 123
    

    Try It Now

  • parseFloat(): Parses a string and returns a floating-point number.
    let num = parseFloat('123.45');
    console.log(num);  // Output: 123.45
    

    Try It Now

  • Number(): Converts a value to a number.
    let num = Number('123.45');
    console.log(num);  // Output: 123.45
    

    Try It Now

5. Checking for Numbers

  • isNaN(): Checks if a value is NaN.
    console.log(isNaN('Hello'));  // Output: true
    

    Try It Now

  • isFinite(): Checks if a value is a finite number.
    console.log(isFinite(10 / 2));  // Output: true
    

    Try It Now

  • Number.isInteger(): Checks if a value is an integer.
    console.log(Number.isInteger(42));  // Output: true
    

    Try It Now

6. Special Number Values

  • NaN: Represents a computational error or an undefined mathematical result.
    console.log(0 / 0);  // Output: NaN
    

    Try It Now

  • Infinity and -Infinity: Result from division by zero or a number too large for JavaScript to handle.
    console.log(1 / 0);  // Output: Infinity
    console.log(-1 / 0);  // Output: -Infinity
    

    Try It Now

7. Example: Basic Arithmetic Operations

let a = 10;
let b = 3;
console.log(a + b);  // Output: 13
console.log(a - b);  // Output: 7
console.log(a * b);  // Output: 30
console.log(a / b);  // Output: 3.3333
console.log(a % b);  // Output: 1

Try It Now

Summary

  • Numbers in JavaScript can represent both integers and floating-point values.
  • Methods like toFixed(), parseInt(), and Number() help in converting and formatting numbers.
  • Special values like NaN and Infinity handle exceptional cases in numerical computations.

These capabilities make JavaScript numbers versatile for a wide range of mathematical operations and data manipulation tasks.