DEV Community

Jennifer Tieu
Jennifer Tieu

Posted on • Updated on

Self-Taught Developer Journal, Day 23: TOP JavaScript Fundamentals Part 1 - Numbers cont.

Today I learned...

Integer Precision

Integers are accurate up to 15 digits.

For decimals, the maximum number of digits is 17. After that, the number will round up.

There is also the BigInt data type that can represent integers that are too big for the Numbers data type.

Float Precision

"The floating point is not always 100% accurate."
In the W3School example,

let x = 0.2 + 0.1;
Enter fullscreen mode Exit fullscreen mode

The output is 0.30000000000000004, not 0.3. To solve it, multiplying and dividing will work.

let x = (0.2 * 10 + 0.1 * 10) / 10;
Enter fullscreen mode Exit fullscreen mode

Adding Numbers and Strings

It seems like the most important thing to remember here is that the "+" operator in JavaScript is used for both addition (numbers) and concatenation (strings).

  • Two numbers added will be a number.
  • Two strings added will be a string concatenation.
  • A string and a number added will be a string concatenation regardless of order (string + number or number + string).

Reminder

JavaScript interpreter works from left to right.

let x = 10;
let y = 20;
let z = "30";
let result = x + y + z;
Enter fullscreen mode Exit fullscreen mode

Output is 3030 because 10 + 20 are added first then the string is concatenated.

Numeric Strings

For numeric strings, "JavaScript will try to convert strings to numbers in all numeric operations".

NaN (Not a Number)

NaN is "a JavaScript reserved word indicating that a number is not a legal number." There is a isNan() function to identify if the value is not a number. NaN is a number type.

Resources

The Odin Project
https://www.w3schools.com/js/js_numbers.asp
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt

Top comments (0)