Why Javascript addition is behaving differently?

Viewed 24

Last day while testing I came across an awkward scenario that I am still unable to figure out why this is occurring. The scenario is simple adding 2 numbers in Javascript. Below are the cases I found:

Case 1:

let a = 2.3333, b = 2; // For b take any number between -2,2,3,4,5
console.log(a+b); // Returns value as 4.3332999999999995. This is type of value is same for the other values of b mentioned above

Case 2:

let a = 2.3333, b = 6; // Any value except -2,2,3,4,5
console.log(a+b); // Returns value as 8.3333. Expected result is coming if I take any values except those mentioned above.

Although Case 1 can be fixed using `toFixed(...)``` but my question is why this is happening? Can anyone explain and are these the only values that will cause this awkward scene?

0 Answers
Related