Integers, which are numbers without a decimal point or exponent notation, remain accurate up to 15 digits.
Example
let x = 999999999999999; // x will be 999999999999999 let y = 9999999999999999; // y will be 10000000000000000 |
The maximum precision for decimal numbers is 17 digits.
Floating-point arithmetic may not always yield entirely accurate results.
let x = 0.2 + 0.1; |
To address the aforementioned issue, it is beneficial to utilize multiplication and division.
let x = (0.2 * 10 + 0.1 * 10) / 10; |
Caution!! JavaScript employs the + operator for both addition and concatenation. Numeric values are added, while strings are concatenated. |
When adding two numbers in JavaScript, the resultant value will also be a number.
Example
let x = 10; let y = 20; let z = x + y; |
When concatenating two strings in JavaScript, the outcome will be a string concatenation.
Example
let x = “10”; let y = “20”; let z = x + y; |
When combining a number and a string in JavaScript, the result will be a string concatenation.
Example
let x = 10; let y = “20”; let z = x + y; |
When combining a string and a number in JavaScript, the result will be a string concatenation.
Example
let x = “10”; let y = 20; let z = x + y; |
A common error is to anticipate the result to be 30.
Example
let x = 10; let y = 20; let z = “The result is: “ + x + y; |
A common mistake is to anticipate the result to be 102030.
Example
let x = 10; let y = 20; let z = “30”; let result = x + y + z; |
The JavaScript interpreter processes expressions from left to right. Initially, it performs addition between 10 and 20, as both x and y are numeric values. Subsequently, concatenation occurs between 30 and “30”, since z is a string. |