In JavaScript, dealing with numbers is a fundamental aspect of programming. Let’s explore some useful techniques and methods for working with numbers efficiently.You already know the primary way of defining number data type is /by simply writing “const score = 100” (here javascript automatically detects that type of score is number), but we can explicitly define a variable type to Number, using “new” keyword and “Number” object.
let’s learn the concept of Number object.
Number object
While primitive numbers satisfy the need for most scenarios, JavaScript also offers the Number object for explicit number declaration. This can be achieved using the new keyword along with the Number object:
const balance = new Number(100);
console.log(balance);
Now we are using the Number object, we also get different methods that we can use that are provided by the Number objects, for eg:
The Number object provides several handy methods for manipulating numbers:
- toString(): Converts a number to a string.
- toFixed(): Formats a number to a fixed-point notation with a specified number of decimal places.
- toPrecision(): Formats a number to a specified precision.
- toLocaleString(): Returns a string representing the number formatted according to the locale-specific conventions.
console.log(balance.toString().length);
console.log(balance.toFixed(2));
const otherNumber = 23.8966;
console.log(otherNumber.toPrecision(3));
const otherNumberNew = 123.3966;
console.log(otherNumberNew.toPrecision(4));
const otherNumberTwo = 1123.8966
console.log(otherNumberTwo.toPrecision(3));
const hundreds = 1000000;
console.log(hundreds.toLocaleString(‘en-IN’));
Maths in JavaScript
The Math object in JavaScript is a built-in global object providing mathematical functions. It offers a range of methods for performing mathematical operations and manipulating numbers with precision and ease.
The Math object in JavaScript provides a range of mathematical constants and functions:
- abs(): Returns the absolute value of a number.
- round(): Rounds a number to the nearest integer.
- ceil(): Rounds a number up to the nearest integer.
- floor(): Rounds a number down to the nearest integer.
- sqrt(): Returns the square root of a number.
- min(): Returns the minimum value among its arguments.
- max(): Returns the maximum value among its arguments.
- random(): Returns a random floating-point number between 0 (inclusive) and 1 (exclusive).
console.log(Math);
console.log(Math.abs(-4));
console.log(Math.round(11.3));
console.log(Math.round(4.6));
console.log(Math.ceil(7.1));
console.log(Math.floor(2.9));
console.log(“square root of 25 is ” + Math.sqrt(25));
console.log(“Minimum value is ” + Math.min(4,3,6,8));
console.log(“Maximum value is ” + Math.max(4,3,6,8));
Generating random numbers in JS
To generate random numbers within a specific range, you can use the Math.random() method in conjunction with appropriate arithmetic:
Math.random() in JavaScript returns a random floating-point number between 0 (inclusive) and 1 (exclusive).
console.log(math.random());
console.log((Math.random() * 10) + 1);
console.log(Math.floor(Math.random() * 10) + 1);
This code generates a random number between 1 and 10.
- Math.random() gives us a random decimal number between 0 (inclusive) and 1 (exclusive).
- We multiply this by 10 to expand the range to 0 (inclusive) to 10 (exclusive), effectively shifting the decimal point one place to the left.
- Finally, we add 1 to ensure that we never get a result of 0 and to make sure the range includes 10.
- Math.floor() rounds down to the nearest whole number, effectively removing the decimal part.
What if we want to print numbers between a certain range
const min = 10;
const max = 20;
console.log(Math.floor(Math.random() * (max – min + 1)) + min);
- Math.random() gives us a random decimal number between 0 (inclusive) and 1 (exclusive).
- We want our random number to fall within the range of 10 to 20. So, we calculate the range by subtracting the minimum value (10) from the maximum value (20), and then add 1 to include the upper limit.
- Multiplying this range by Math.random() gives us a random decimal number between 0 (inclusive) and the size of our range (inclusive).
- Math.floor() rounds down this decimal number to the nearest whole number.
- Finally, we add the minimum value (10) to ensure that the random number falls within our desired range.
Source: hashnode.com