syntax: .toString()
examples:
let randomNumber = 3124325454352;
console.log(randomNumber);
console.log(randomNumber.toString());
console.log((21414 + 234234).toString());
Console view:
In the console image, you’ll see that before we converted it to a string, randomNumber was printed as a numeric value, without quotation marks.
Remember that when numbers are concatenated to strings, they become strings themselves. So, there is no reason to use the .toString method during string to number concatenation.
This method comes in handy when using functions or methods that require a string parameter and thus the number needs to be converted to a string.
syntax: .toExponential(n)
examples:
let x = 3.2526;
console.log(x);
console.log(x.toExponential(2));
console.log(x.toExponential(4));
Console view:
syntax: .toFixed(n)
examples:
let payCheck = 3000;
let groceries = 240.247;
let utilityBill = 139.26;
let cellPhone = 45.206;
let savings = 1000;
let spendingMoney = payCheck - groceries - utilityBill - cellPhone - savings;
console.log("This is how much money I have left: $" + spendingMoney);
console.log("But it makes more sense to round money to two decimal places, like this : $" + spendingMoney.toFixed(2));
Console view:
syntax: .toPrecision(n)
examples:
let grade = 87.2267;
console.log("My grade is " + grade.toPrecision(4));
console.log("My grade is " + grade.toPrecision(3));
console.log("My grade is " + grade.toPrecision(2));
Console view:
See the Pen Exercise 3.5 by LSU DDEM (@lsuddem) on CodePen.