JavaScript Numbers

JavaScript Numbers

JavaScript Numbers

In JavaScript, numbers are always stored as floating-point values (even integers). Unlike other languages, there’s no separate type for int and float. Let’s dive into how JavaScript handles numbers and their built-in methods! 

1️⃣ Declaring Numbers

let num1 = 42; // Integer let num2 = 3.14; // Floating-point number let num3 = 1e6; // Exponential notation (1 million) let num4 = 0b1010; // Binary (10 in decimal) let num5 = 0o52; // Octal (42 in decimal) let num6 = 0x2A; // Hexadecimal (42 in decimal)

📌 JavaScript automatically converts between integer and floating-point values.

2️⃣ Number Methods

🔹 toFixed(n) – Rounds to n decimal places (returns a string)

let num = 3.14159; console.log(num.toFixed(2)); // "3.14" console.log(num.toFixed(0)); // "3"

🔹 toPrecision(n) – Formats number to n significant digits

let num = 123.456; console.log(num.toPrecision(4)); // "123.5" console.log(num.toPrecision(2)); // "1.2e+2" (scientific notation)

🔹 toString(base) – Converts number to a string in a given base

let num = 255; console.log(num.toString(2)); // "11111111" (binary) console.log(num.toString(16)); // "ff" (hexadecimal)

🔹 Number.isInteger(value) – Checks if a number is an integer

console.log(Number.isInteger(10)); // true console.log(Number.isInteger(10.5)); // false

🔹 Number.isNaN(value) – Checks if a value is NaN


console.log(Number.isNaN(0 / 0)); // true console.log(Number.isNaN("hello" / 2)); // true console.log(Number.isNaN(42)); // false

3️⃣ Special Number Values

🔹 Infinity & -Infinity

console.log(1 / 0); // Infinity console.log(-1 / 0); // -Infinity

🔹 NaN (Not a Number)

Occurs when an invalid mathematical operation is performed.

console.log("hello" * 5); // NaN console.log(Math.sqrt(-1)); // NaN console.log(0 / 0); // NaN

📌 NaN is the only value that is not equal to itself:

console.log(NaN === NaN); // false

Use Number.isNaN(value) instead of value === NaN.

4️⃣ Math Object – Useful Methods

The Math object provides built-in functions for mathematical operations.

🔹 Math.round() – Rounds to nearest integer

console.log(Math.round(4.6)); // 5 console.log(Math.round(4.4)); // 4

🔹 Math.floor() – Rounds down

console.log(Math.floor(4.9)); // 4 console.log(Math.floor(-4.1)); // -5

🔹 Math.ceil() – Rounds up

console.log(Math.ceil(4.1)); // 5 console.log(Math.ceil(-4.9)); // -4

🔹 Math.trunc() – Removes decimal part

console.log(Math.trunc(4.9)); // 4 console.log(Math.trunc(-4.9)); // -4

🔹 Math.random() – Generates a random number between 0 and 1

console.log(Math.random()); // Example: 0.6754321

To get a random integer in a range:

function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } console.log(getRandomInt(1, 10)); // Random number between 1 and 10

🔹 Math.pow(base, exponent) – Power function

console.log(Math.pow(2, 3)); // 8 console.log(2 ** 3); // 8 (ES6 shorthand)

🔹 Math.sqrt(n) – Square root

console.log(Math.sqrt(25)); // 5 console.log(Math.sqrt(2)); // 1.414

🔹 Math.abs(n) – Absolute value

console.log(Math.abs(-5)); // 5

🔹 Math.max() & Math.min()

console.log(Math.max(10, 20, 5)); // 20 console.log(Math.min(10, 20, 5)); // 5

5️⃣ Converting Strings to Numbers

🔹 Number(value) – Converts string to number

console.log(Number("42")); // 42 console.log(Number("3.14")); // 3.14 console.log(Number("hello")); // NaN

🔹 parseInt(value, base) – Converts string to integer

console.log(parseInt("100px")); // 100 console.log(parseInt("101", 2)); // 5 (binary to decimal)

🔹 parseFloat(value) – Converts string to floating-point number

console.log(parseFloat("3.14px")); // 3.14 console.log(parseFloat("100")); // 100

📌 parseInt() and parseFloat() ignore non-numeric characters after a valid number, while Number() returns NaN if the input contains non-numeric characters.

6️⃣ Handling Precision Issues

JavaScript has floating-point precision issues:

console.log(0.1 + 0.2); // 0.30000000000000004 ❌

🔹 Fix using toFixed()

let result = (0.1 + 0.2).toFixed(2); console.log(result); // "0.30" console.log(Number(result)); // 0.3

7️⃣ Checking for Finite Numbers

🔹 Number.isFinite(value)

Checks if a value is a finite number (not NaN, Infinity, or -Infinity).

console.log(Number.isFinite(42)); // true console.log(Number.isFinite(Infinity)); // false console.log(Number.isFinite(NaN)); // false console.log(Number.isFinite("100")); // false

8️⃣ Summary

All numbers in JavaScript are floating-point
Use toFixed() and toPrecision() to format numbers
Use Math functions for advanced calculations
Use Number(), parseInt(), and parseFloat() for conversions
Be mindful of floating-point precision issues

🚀 Now you're a pro at handling numbers in JavaScript! Let me know if you need more examples. 😊

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close