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 aNumber
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.
- 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 ofx
.Math.round(x)
: Roundsx
to the nearest integer.Math.floor(x)
: Roundsx
down to the nearest integer.Math.ceil(x)
: Roundsx
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.
Playlist of JS-Journey (JavaScript)
1. Introduction to JavaScript - What, Why, and How? | JavaScript Journey | In Hindi | Death CodeWatch
2. Variables & Comments in JavaScript π | JavaScript Journey | In Hindi | Death CodeWatch
3. Understanding Data Types in JavaScript π | JavaScript Journey | In Hindi | Death CodeWatch
4. Master Type Conversion & Reference Data Types π | JavaScript Journey | In Hindi | Death CodeWatch
5. Mastering Operators in JavaScript π | JavaScript Journey | In Hindi | Death CodeWatch
6. Mastering Numbers & Math Functions π | JavaScript Journey | In Hindi | Death CodeCurrently Playing
7. Strings and String Methods in JavaScript π | JavaScript Journey | In Hindi | Death CodeWatch
8. Mastering Dates and Date Methods in JavaScript β³ | JavaScript Journey | In Hindi | Death CodeWatch
9. JavaScript Arrays & Array Methods Explained β³ | JavaScript Journey | In Hindi | Death CodeWatch
10. Objects and Object Methods Explained π | JavaScript Journey | In Hindi | Death CodeWatch
11. Conditional Statements | If, Switch & Ternary Operator | JavaScript Journey | In Hindi | Death CodeWatch
12. Mastering Loops (for, while, do-while) | JavaScript Journey | In Hindi | Death CodeWatch
13. Understanding Functions | JavaScript Journey | In Hindi | Death CodeWatch
14. HOFs, Callback Functions, Arrow Functions & More! π | JavaScript Journey | In Hindi | Death CodeWatch
15. Mastering Scope & Hoisting Explained in Hindi π₯ | JavaScript Journey | In Hindi | Death CodeWatch
16. Map, Filter, Reduce & forEach Explainedπ₯ | JavaScript Journey | In Hindi | Death CodeWatch
17. Understanding this Keyword, Function Context & IIFEπ₯ | JavaScript Journey | In Hindi | Death CodeWatch
18. Dive into Call Stack & JavaScript Execution Contextπ₯ | JavaScript Journey | In Hindi | Death CodeWatch
19. Master Async Await & Event Loop π₯ | JavaScript Journey | In Hindi | Death CodeWatch
20. Promises in JavaScript Made Easy π | JavaScript Journey | In Hindi | Death CodeWatch
21. Fetch Function in JavaScript π | JavaScript Journey | In Hindi | Death CodeWatch
22. JavaScript OOP Basics π | JavaScript Journey | In Hindi | Death CodeWatch
23. Mastering Prototypes & Inheritance in JS π | JavaScript Journey | In Hindi | Death CodeWatch
24. JavaScript Classes & Inheritance Explained! π | JavaScript Journey | In Hindi | Death CodeWatch
25. Mastering Getter and Setter (Why & How to use them?) π | JavaScript Journey | In Hindi | Death CodeWatch
26. JavaScript Property Descriptors π | JavaScript Journey | In Hindi | Death CodeWatch
27. DOM Basics π | JavaScript Journey | In Hindi | Death CodeWatch
28. How to Create, Update, and Delete DOM Elements π | JavaScript Journey | In Hindi | Death CodeWatch
29. JavaScript Events & Propagation π | JavaScript Journey | In Hindi | Death CodeWatch