*
Previous Strings & Template Literals Conversion & Coercion in JavaScript Next

JavaScript Numbers & Math Object

πŸ“˜ JavaScript Numbers & Math Object

1️⃣ Numbers in JavaScript

JavaScript has only one type of number: double-precision 64-bit floating point (same as IEEE 754 standard). It can represent both integers and decimals.

    let x = 10;       // integer
    let y = 3.14;     // floating point
    let z = 2e5;      // scientific notation (200000)
    let big = 999999999999999;   // precision limit (15 digits)
    let notANum = "abc" / 2;     // NaN (Not a Number)
    let inf = 1 / 0;             // Infinity

    // Operations
    console.log(10 + 5); // 15
    console.log(10 - 5); // 5
    console.log(10 * 5); // 50
    console.log(10 / 3); // 3.3333333333333335
    console.log(10 % 3); // 1 (remainder)

πŸ”’ Special Number Values

  • Infinity β†’ result of division by zero
  • -Infinity β†’ negative division by zero
  • NaN β†’ Not a Number (invalid operations)
console.log(1 / 0);     // Infinity
console.log(-1 / 0);    // -Infinity
console.log("abc" / 2); // NaN

2️⃣ Number Methods

Numbers come with useful methods:

let num = 123.456;

console.log(num.toFixed(2));   // "123.46" (rounds to 2 decimals)
console.log(num.toString());   // "123.456" (convert to string)
console.log(Number("42"));     // 42 (convert string to number)
console.log(parseInt("12.34")); // 12
console.log(parseFloat("12.34")); // 12.34
  

3️⃣ Math Object

The Math object provides properties and methods for mathematical operations. It is static, so you don’t create it with new.

Math Constants

console.log(Math.PI);    // 3.141592653589793
console.log(Math.E);     // 2.718281828459045
console.log(Math.SQRT2); // 1.4142135623730951
  

Math Methods

  • Math.round(x) β†’ Round to nearest integer
  • Math.ceil(x) β†’ Round up
  • Math.floor(x) β†’ Round down
  • Math.trunc(x) β†’ Remove decimal part
  • Math.pow(x, y) β†’ x to the power y
  • Math.sqrt(x) β†’ Square root
  • Math.abs(x) β†’ Absolute value
  • Math.min(...values) β†’ Smallest value
  • Math.max(...values) β†’ Largest value
  • Math.random() β†’ Random number between 0 and 1
console.log(Math.round(4.6));   // 5
console.log(Math.ceil(4.1));    // 5
console.log(Math.floor(4.9));   // 4
console.log(Math.trunc(4.9));   // 4
console.log(Math.pow(2, 3));    // 8
console.log(Math.sqrt(64));     // 8
console.log(Math.abs(-7));      // 7
console.log(Math.min(3, 7, 1)); // 1
console.log(Math.max(3, 7, 1)); // 7
console.log(Math.random());     // random between 0 and 1
  

βœ… Example: Using Math with Numbers

let radius = 5;
let area = Math.PI * Math.pow(radius, 2);
console.log("Area of circle: " + area);

4️⃣ Mini Project: Random Number Generator

Generate a random integer between 1 and 10:

let randomNum = Math.floor(Math.random() * 10) + 1;
console.log("Random number: " + randomNum);
  

βœ… Quick Recap

  • JavaScript numbers are all floating-point (no separate int/float types).
  • Special values: NaN, Infinity, -Infinity.
  • Math object provides constants and methods for calculations.
  • Use Math.random() with Math.floor() to generate random integers.
  • Be cautious with floating-point arithmetic due to precision issues.
Back to Index
Previous Strings & Template Literals Conversion & Coercion in JavaScript Next
*
*