JS Tutorial



JS NUMBERS


JavaScript Numbers

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).

๐Ÿ“Œ Examples of Numbers

  • Integer: let x = 10;
  • Floating Point: let y = 3.14;
  • Negative: let z = -5;

๐Ÿงช Try It Yourself:

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)
  

๐Ÿ“ Number Methods

  • 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).

๐Ÿ”ง Live Example:



๐Ÿ’ก Tips

  • JavaScript automatically converts between number types when needed.
  • Beware of floating point precision issues, e.g., 0.1 + 0.2 โ‰  0.3 exactly.
  • Use 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.


๐ŸŒŸ Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review