DEV Community

Jennifer Tieu
Jennifer Tieu

Posted on • Edited on

1

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

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more