Mastering Numbers & Math Functions πŸš€ | JavaScript Journey | In Hindi | Death Code - DeathCode

JavaScript Math Module and Number Data Type Documentation

JavaScript provides a built-in Math module that contains various mathematical constants and functions. Additionally, the Number data type is used to represent both integer and floating-point numbers.

1. Number Data Type

The Number data type in JavaScript can represent both whole numbers and decimals. It is important to note that JavaScript uses double-precision 64-bit binary format (IEEE 754) for all numbers.

Example Code

 
let age = 503450.928374; // A floating-point number
let id = new Number(2000000); // Creating a Number object
 
// console.log(age); // Outputs: 503450.928374
// console.log(id); // Outputs: [Number: 2000000]
 
// Formatting and converting numbers
// console.log(id.toLocaleString('en-IN')); // Outputs: "20,00,000" (Indian number format)
// console.log(id.toString()); // Outputs: "2000000"
// console.log(age.toFixed(2)); // Outputs: "503450.93" (rounds to 2 decimal places)
// console.log(age.toPrecision(5)); // Outputs: "503450.9" (5 significant digits)
     

Explanation

  • age is a floating-point number representing a precise value.
  • id is created as a Number object, which can be useful for certain methods.
  • toLocaleString('en-IN') formats the number according to the Indian numbering system.
  • toString() converts the number to a string representation.
  • toFixed(2) rounds the number to two decimal places.
  • toPrecision(5) formats the number to five significant digits.
  1. Math Module

The Math module provides a variety of mathematical functions and constants.

Commonly Used Math Functions

  • Math.PI : The value of Ο€ (pi).
  • Math.abs(x) : Returns the absolute value of x.
  • Math.round(x) : Rounds x to the nearest integer.
  • Math.floor(x) : Rounds x down to the nearest integer.
  • Math.ceil(x) : Rounds x up to the nearest integer.
  • Math.random() : Returns a random floating-point number between 0 (inclusive) and 1 (exclusive).

Example Code

 
// Math Module Example
 
// console.log(Math.PI); // Outputs: 3.141592653589793
// console.log(Math.abs (-565)); // Outputs: 565 (absolute value)
// console.log(Math.round(7.28)); // Outputs: 7 (rounded to nearest integer)
// console.log(Math.floor(7.999)); // Outputs: 7 (rounded down)
// console.log(Math.ceil(7.111)); // Outputs: 8 (rounded up)
 
let luckyNumber = Math.random(); // Generates a random number between 0 and 1
luckyNumber *= 10; // Scales the number to 0-10
luckyNumber = Math.floor(luckyNumber) + 10; // Rounds down and adds 10 (10-19)
// console.log(luckyNumber); // Outputs a random number between 10 and 19
 
let min = 10, max = 30;
let random = Math.floor(Math.random() * (max - min + 1)) + min; // Generates a random number between 10 and 30
console.log(random); // Outputs a random number between 10 and 30
     

Explanation

  • Math.PI provides the value of Ο€, which is approximately 3.14.
  • Math.abs(-565) returns the absolute value of -565, which is 565.
  • Math.round(7.28) rounds the number to the nearest integer, resulting in 7.
  • Math.floor(7.999) rounds down to the nearest integer, resulting in 7.
  • Math.ceil(7.111) rounds up to the nearest integer, resulting in 8.
  • Math.random() generates a random number between 0 and 1. The subsequent operations scale and adjust this number to create a random number within specified ranges.

Conclusion

The Math module and Number data type are essential components of JavaScript that allow for complex mathematical operations and number manipulations. Understanding these concepts is crucial for effective programming in JavaScript.

Β© 2024 DeathCode. All Rights Reserved.