Number

A Number is a data type used to represent numerical values. In JavaScript, the Number type is used to handle both integer and floating-point values. JavaScript supports various operations on numbers, such as arithmetic operations, comparisons, and conversion between different types.

Examples:

JavaScript:

  1. Creating Numbers:

    let integer = 42;
    let floatingPoint = 3.14;
    
  2. Arithmetic Operations:

    let sum = 10 + 5;         // Addition
    let difference = 10 - 5;  // Subtraction
    let product = 10 * 5;     // Multiplication
    let quotient = 10 / 5;    // Division
    let remainder = 10 % 3;   // Modulus (remainder)
    
    console.log(sum);         // Output: 15
    console.log(difference);  // Output: 5
    console.log(product);     // Output: 50
    console.log(quotient);    // Output: 2
    console.log(remainder);   // Output: 1
    
  3. Math Methods:

    let number = 3.14159;
    
    // Rounding
    console.log(Math.round(number));   // Output: 3
    console.log(Math.floor(number));   // Output: 3
    console.log(Math.ceil(number));    // Output: 4
    
    // Power and Square Root
    console.log(Math.pow(2, 3));       // Output: 8 (2^3)
    console.log(Math.sqrt(16));        // Output: 4
    
    // Random Number
    console.log(Math.random());        // Output: Random number between 0 and 1
    
  4. Number Conversions:

    let stringNumber = "42";
    let convertedNumber = Number(stringNumber);
    console.log(convertedNumber);      // Output: 42
    
    let parsedInteger = parseInt("42");
    let parsedFloat = parseFloat("3.14");
    console.log(parsedInteger);        // Output: 42
    console.log(parsedFloat);          // Output: 3.14
    
  5. Handling Special Numeric Values:

    let infinity = Infinity;
    let negativeInfinity = -Infinity;
    let notANumber = NaN;
    
    console.log(infinity);             // Output: Infinity
    console.log(negativeInfinity);     // Output: -Infinity
    console.log(notANumber);           // Output: NaN