3.1 Number Type
Numbers in JavaScript play a crucial role when working with data and performing calculations. The language provides a lot of possibilities for handling numbers, including various operations and built-in methods.
In JavaScript, there's one data type for numbers — Number. This data type represents both integers and floating-point numbers. Additionally, starting from 2020, the BigInt type was introduced for handling very large integers.
Examples:
let integer = 42; // Integer
let float = 3.14; // Floating-point number
let bigInt = 12345678901234567890n; // Big integer
Operations with Numbers
Arithmetic operations:
Operation | Syntax | Description | Example | Result |
---|---|---|---|---|
Addition | a + b | Adds two numbers | 5 + 3 | 8 |
Subtraction | a - b | Subtracts one number from another | 10 - 4 | 6 |
Multiplication | a * b | Multiplies two numbers | 7 * 3 | 21 |
Division | a / b | Divides one number by another | 20 / 5 | 4 |
Remainder | a % b | Returns the remainder of division | 10 % 3 | 1 |
Exponentiation | a ** b | Raises a number to a power | 2 ** 3 | 8 |
Increment and Decrement
Operation | Syntax | Description | Example | Result |
---|---|---|---|---|
Increment | ++a | Increases value by one (prefix) | let a = 5; ++a; |
6 |
Increment | a++ | Increases value by one (postfix) | let a = 5; a++; |
5 (and then 6) |
Decrement | --b | Decreases value by one (prefix) | let b = 5; --b; |
4 |
Decrement | b-- | Decreases value by one (postfix) | let b = 5; b--; |
5 (and then 4) |
Combined Operations
Operation | Syntax | Description | Example | Result |
---|---|---|---|---|
Addition Assignment | a += b | Adds and assigns the result | let x = 10; x += 5; |
15 |
Subtraction Assignment | a -= b | Subtracts and assigns the result | let x = 10; x -= 3; |
7 |
Multiplication Assignment | a *= b | Multiplies and assigns the result | let x = 10; x *= 2; |
20 |
Division Assignment | a /= b | Divides and assigns the result | let x = 10; x /= 2; |
5 |
Remainder Assignment | a %= b | Finds remainder and assigns the result | let x = 10; x %= 3; |
1 |
3.2 Built-in Methods
JavaScript has a built-in Math
object for performing various mathematical operations.
1. Math.round():
Rounds a number to the nearest integer.
let rounded = Math.round(4.6); // 5
2. Math.ceil():
Rounds a number upwards to the nearest integer.
let ceil = Math.ceil(4.2); // 5
3. Math.floor():
Rounds a number downwards to the nearest integer.
let floor = Math.floor(4.8); // 4
4. Math.random():
Returns a random number between 0 and 1.
let random = Math.random();
5. Math.max() and Math.min():
Return the maximum and minimum value from a set of numbers.
let max = Math.max(1, 2, 3, 4); // 4
let min = Math.min(1, 2, 3, 4); // 1
6. Math.sqrt():
Returns the square root of a number.
let sqrt = Math.sqrt(16); // 4
7. Math.pow():
Returns a number raised to a specified power.
let pow = Math.pow(2, 3); // 8
3.3 Parsing Strings to Numbers
Function parseInt():
Converts a string into an integer.
let int = parseInt('123'); // 123
Function parseFloat():
Converts a string into a floating-point number.
let float = parseFloat('3.14'); // 3.14
Function Number():
Converts a value into a number.
let num = Number('42'); // 42
Value Checking
Function isNaN():
Checks if the value is NaN (Not-a-Number).
let isNotNumber = isNaN('hello'); // true
Function isFinite():
Checks if the value is a finite number (whether it's infinity or not).
let finite = isFinite(10); // true
let notFinite = isFinite(Infinity); // false
3.4 Number Features in JavaScript
1. Number Limits
In JavaScript, numbers are represented in double-precision floating-point format, which has certain limitations:
- Maximum value: Number.MAX_VALUE
- Minimum value: Number.MIN_VALUE
- Positive infinity: Infinity
- Negative infinity: -Infinity
- Not a number (NaN): a value that isn't a number
2. Number Precision
Working with floating-point numbers can lead to precision issues:
let sum = 0.1 + 0.2;
console.log(sum); // 0.30000000000000004
This happens in all programming languages – JavaScript is no exception here.
3. Safe Integers
JavaScript has the concept of "safe" integers, which can be accurately represented as floating-point numbers:
- Maximum safe integer:
Number.MAX_SAFE_INTEGER (253 - 1)
- Minimum safe integer:
Number.MIN_SAFE_INTEGER (-(253 - 1))
GO TO FULL VERSION