Programming Language
JavaScript
Number

Numbers in JavaScript

Integers, Floating Points, and Number Literals

Integers and Floating Points

JavaScript numbers are floating-point by default, meaning they can store both integers and decimal numbers.

Example:

let intNumber = 42;  // Integer
let floatNumber = 3.14; // Floating-point number

Number Literals

JavaScript supports different number literals:

  • Decimal (Base 10): 123
  • Binary (Base 2): 0b1010 (10 in decimal)
  • Octal (Base 8): 0o52 (42 in decimal)
  • Hexadecimal (Base 16): 0x2A (42 in decimal)

Example:

console.log(0b1010); // 10
console.log(0o52); // 42
console.log(0x2A); // 42

Arithmetic Operators

JavaScript provides basic arithmetic operators:

OperatorDescriptionExample
+Addition5 + 3 // 8
-Subtraction10 - 4 // 6
*Multiplication6 * 7 // 42
/Division20 / 5 // 4
%Modulus (Remainder)10 % 3 // 1
**Exponentiation2 ** 3 // 8

Example:

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.3333
console.log(a % b); // 1
console.log(a ** b); // 1000

Exploring toFixed() and Math.round()

toFixed()

The toFixed(n) method formats a number to n decimal places, returning a string.

Example:

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

Math.round()

Rounds a number to the nearest integer.

Example:

console.log(Math.round(3.5)); // 4
console.log(Math.round(3.4)); // 3

Mastering Math.floor(), Math.ceil(), and Math.random()

Math.floor()

Rounds down to the nearest integer.

console.log(Math.floor(3.9)); // 3

Math.ceil()

Rounds up to the nearest integer.

console.log(Math.ceil(3.1)); // 4

Math.random()

Generates a random number between 0 and 1.

console.log(Math.random()); // e.g., 0.7234

Generating a random integer between min and max:

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

Understanding parseInt() and parseFloat()

parseInt()

Converts a string to an integer, ignoring decimals.

console.log(parseInt("42px")); // 42
console.log(parseInt("3.14")); // 3

parseFloat()

Converts a string to a floating-point number.

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

Understanding Infinity, Number.MAX_VALUE, MAX_SAFE_INTEGER, MIN_SAFE_INTEGER

Infinity and -Infinity

Represents numbers too large for JavaScript to handle.

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

Number.MAX_VALUE and Number.MIN_VALUE

Represents the largest and smallest possible numbers.

console.log(Number.MAX_VALUE); // 1.7976931348623157e+308
console.log(Number.MIN_VALUE); // 5e-324

Number.MAX_SAFE_INTEGER and Number.MIN_SAFE_INTEGER

The largest and smallest safe integers in JavaScript.

console.log(Number.MAX_SAFE_INTEGER); // 9007199254740991
console.log(Number.MIN_SAFE_INTEGER); // -9007199254740991

Number Formatting in JavaScript: Intl.NumberFormat and toLocaleString()

toLocaleString()

Formats numbers based on locale.

let num = 1234567.89;
console.log(num.toLocaleString("en-US")); // "1,234,567.89"
console.log(num.toLocaleString("de-DE")); // "1.234.567,89"

Intl.NumberFormat

Provides advanced number formatting.

let formatter = new Intl.NumberFormat("en-US", { style: "currency", currency: "USD" });
console.log(formatter.format(1234567.89)); // "$1,234,567.89"

Understanding NaN (Not a Number)

NaN represents an invalid number.

console.log(0 / 0); // NaN
console.log(parseInt("abc")); // NaN
console.log(Math.sqrt(-1)); // NaN

To check for NaN, use:

console.log(isNaN("hello")); // true
console.log(Number.isNaN(NaN)); // true
console.log(Number.isNaN("hello")); // false (better check)

Conclusion

JavaScript provides powerful number handling features, including various arithmetic operations, rounding methods, parsing functions, and formatting options. Mastering these will help you handle numbers efficiently in your applications.