What Are JavaScript Numbers? See Math Methods

Numbers are one of the core data types in JavaScript. You can perform arithmetic operations, rounding, and use built-in Math methods to manipulate numbers efficiently.

This blog explains numbers, operations, and useful Math methods with examples.


1. What Is a Number?

In JavaScript, numbers can be integers or floating-point values.

let age = 22;       // integer
let price = 99.99;  // floating-point
console.log(age, price);

2. Arithmetic Operations

JavaScript supports basic arithmetic:

let a = 10, 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 (remainder)

3. Increment & Decrement

let x = 5;
x++; // 6
x--; // 5
console.log(x);

4. JavaScript Math Object

The Math object provides built-in mathematical functions.

Math.round()

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

Math.floor() & Math.ceil()

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

Math.sqrt()

console.log(Math.sqrt(16)); // 4

Math.pow()

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

Math.random()

console.log(Math.random()); // Random number between 0 and 1
console.log(Math.floor(Math.random() * 10) + 1); // Random number 1-10

5. Converting Numbers

String to Number

let str = "123";
let num = Number(str);
console.log(num); // 123

Number to String

let num = 456;
let str = num.toString();
console.log(str); // "456"

6. Checking Numbers

console.log(Number.isInteger(10)); // true
console.log(Number.isNaN(NaN));    // true
console.log(Number.isFinite(100)); // true

Conclusion

Numbers are a fundamental part of JavaScript programming. Understanding arithmetic operations, Math methods, and number conversions is crucial for building robust and dynamic web applications.


📌 Citations

🔗 View other articles about Javascript:
https://savanka.com/category/learn/js/

🔗 External Javascript Documentation:
https://www.w3schools.com/js/

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *