JavaScript Numbers

JavaScript Numbers
In JavaScript, numbers are used to perform mathematical operations such as addition, subtraction, multiplication, division, and more. Unlike many programming languages, JavaScript uses only one numeric type for all numbers — whether integers, decimals, or scientific values.
Examples of Numbers in Js
Basic Arithmetic with Numbers
JavaScript supports basic arithmetic operators:
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | Addition | 10 + 5 | 15 |
- | Subtraction | 10 - 5 | 5 |
* | Multiplication | 10 * 5 | 50 |
/ | Division | 10 / 5 | 2 |
% | Modulus (Remainder) | 10 % 3 | 1 |
** | Exponentiation | 2 ** 3 | 8 |
Example:
Number Methods
Js provides built-in methods to work with numbers:
| Method | Description | Example | Result |
|---|---|---|---|
toString() | Converts number to string | (20).toString() | "20" |
toFixed(n) | Fixes decimal places | (3.14159).toFixed(2) | "3.14" |
toPrecision(n) | Sets total digits | (3.14159).toPrecision(3) | "3.14" |
parseInt() | Converts string → integer | parseInt("25px") | 25 |
parseFloat() | Converts string → decimal | parseFloat("25.5kg") | 25.5 |
Example:
NaN (Not a Number)
NaN is a special numeric value that indicates an invalid mathematical result.
Infinity and -Infinity
Js numbers can go beyond limit values:
Random Numbers
Using Math.random() you can generate random values:
Example: random number between 1 and 10:
Summary
Js treats all numbers (whole or decimal) as a single type.
You can perform mathematical operations using arithmetic operators.
Useful number methods include
toFixed,parseInt, andNumber().Special values include
NaN,Infinity, and-Infinity.JavaScript provides random number generation using
Math.random().
