JavaScript Integer
In JavaScript, an Integer is a whole number without any decimals. It can be positive, negative, or zero. Integers are widely used in programming for counting, indexing, loops, and arithmetic operations. Although JavaScript treats all numbers as Number type, integers have specific properties and common use cases.
Key Points:
- An integer is a whole number (without a decimal part).
- It can be positive, negative, or zero.
- JavaScript uses the same type for integers and floats (
Number). - For very large integers, BigInt is used.
- Integers are commonly used in loops, counters, and array indexing.
1. Integer Examples in JS
let age = 25; // positive integer let temperature = -5; // negative integer let count = 0; // zero console.log(typeof age); // "number"
2. Integer Properties in JS
- Safe Integer Range: Number.MAX_SAFE_INTEGER → 9007199254740991
- Number.MIN_SAFE_INTEGER → -9007199254740991
- Numbers outside this range may lose precision.
- BigInt: Used for integers larger than safe range. Example:
let bigNum = 123456789012345678901234567890n;
3. Arithmetic Operations
let a = 10; let b = 3; console.log(a + b); // 13 console.log(a - b); // 7 console.log(a * b); // 30 console.log(a / b); // 3.3333333333333335 (float result) console.log(a % b); // 1 (modulus) console.log(a ** b); // 1000 (exponentiation)
4. Increment & Decrement
let x = 5; x++; // x = 6 x--; // x = 5
5. Unary Plus / Minus
let y = "10"; let z = +y; // converts string "10" to number 10 console.log(typeof z); // "number"
6. Integer Checking Methods
- Number.isInteger(value) – checks if a value is an integer
- Math.floor(value) – converts number to nearest lower integer
- Math.ceil(value) – converts number to nearest higher integer
- Math.round(value) – rounds number to nearest integer
- parseInt(string) – converts string to integer
console.log(Number.isInteger(10)); // true console.log(Number.isInteger(10.5)); // false console.log(Math.floor(10.7)); // 10 console.log(Math.ceil(10.3)); // 11 console.log(Math.round(10.5)); // 11 let str = "123"; let num = parseInt(str); console.log(num); // 123
7. BigInt for Large Integers
let bigNum = 123456789012345678901234567890n; console.log(typeof bigNum); // "bigint"
8. Integer vs Float Comparison
| Feature | Integer | Float |
|---|---|---|
| Decimal part | None | Exists |
| Type in JS | Number | Number |
| Examples | 5, -10, 0 | 3.14, -5.5 |
| Operations | + - * / % ** | + - * / |
| Precision | Exact (within safe range) | Approximate (due to decimals) |
9. Real-life Usage
- Counting items in a list:
cart.length - Loop iteration:
for(let i = 0; i < 10; i++) { } - Age, marks, temperature, scores
- Flags: 0 = false, 1 = true
- Array indexing:
arr[0], arr[1]
10. Tips & Notes
- In JavaScript, integer and float share the same type, but usage and operations differ.
- Division may produce a float; for exact integer, use
Math.floor()orMath.round(). - For very large numbers, using
BigIntis safe. - Always remember
Number.isInteger()for programming and exams. - Integers form the base of programming – widely used in loops, counters, indexes, and calculations.
Comments
Post a Comment