JavaScript numbers represent both integers and floating-point values using the same Number type. There is no separate type for integers and decimals โ it's all just Number
.
Note: JavaScript follows the IEEE 754 standard for representing numbers. It can handle values up to ยฑ(253 - 1)
.
let x = 10;
let y = 3.14;
let z = -5;
Open your browser console and try the following code:
let a = 5; let b = 2.5; console.log(a + b); // 7.5 console.log(a * b); // 12.5 console.log(a / 0); // Infinity console.log("10" / 2); // 5 (automatic type conversion)
toFixed(n)
โ Rounds the number to n
decimal places.toString()
โ Converts the number to a string.parseInt()
โ Parses a string and returns an integer.parseFloat()
โ Parses a string and returns a float.isNaN()
โ Checks if the value is NaN
(Not-a-Number).0.1 + 0.2 โ 0.3
exactly.Number.isNaN()
to check for real NaN
values.Try This:
Type typeof 123
or typeof '123'
in the console to see how JavaScript differentiates between a number and a string.
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!