JavaScript Numbers

In JavaScript, numbers are a primitive data type used to represent numeric values. They can be integers (whole numbers) or floating-point numbers (numbers with decimal places).

JavaScript also has a special value called NaN (Not a Number) that represents an undefined or unrepresentable value.

Additionally, there are special values Infinity and -Infinity representing positive and negative infinity.

Integer Precision in JavaScript

In JavaScript, integers are stored as 64-bit floating-point numbers, which means they have a finite precision. This means that integers with large values may lose precision when stored or used in calculations.

For example, the maximum safe integer in JavaScript is 2^53 - 1, and any integers larger than this may lose precision when stored or used in calculations.

Additionally, JavaScript uses the IEEE 754 standard for floating-point arithmetic, which can lead to unexpected results when working with decimal numbers.

For example, not all decimal numbers can be represented exactly as floating-point numbers, which can cause rounding errors and inaccuracies in calculations.

So, it's important to be aware of the limitations of precision when working with integers and floating-point numbers in JavaScript.

Here's an example of how integer precision can be an issue in JavaScript:

let x = 9007199254740992; // this is the largest safe integer in JavaScript
let y = 9007199254740992 + 1;
console.log(x); // 9007199254740992
console.log(y); // 9007199254740992 
console.log(x === y); // true

As you can see, even though x and y have different values, they are stored as the same number due to precision loss. If you try to compare them, you will get true.

To avoid this problem, you can use libraries like big.js, decimal.js or bignumber.js that provide a more precise decimal arithmetic. Here's an example of how to use the bignumber.js library:

const BigNumber = require('bignumber.js');
let x = new BigNumber(9007199254740992);
let y = new BigNumber(9007199254740992).plus(1);
console.log(x.toString()); // 9007199254740992
console.log(y.toString()); // 9007199254740993
console.log(x.eq(y)); // false

As you can see, using bignumber.js library, x and y are different numbers and the comparison returns false.

Please note that, these libraries are slower than JavaScript's built-in number type, so use them only when you need to perform more precise arithmetic.

Floating Precision in JavaScript

In JavaScript, floating-point numbers are also stored as 64-bit numbers and use the IEEE 754 standard for floating-point arithmetic. This means that they have a finite precision and can lead to unexpected results when working with decimal numbers.

For example, not all decimal numbers can be represented exactly as floating-point numbers, which can cause rounding errors and inaccuracies in calculations.

Here is an example of how floating point precision can be an issue:

let x = 0.1 + 0.2;
console.log(x); // 0.30000000000000004

As you can see, the result of 0.1 + 0.2 should be 0.3 but due to the way that JavaScript stores floating-point numbers, it is not exactly 0.3 but a very close approximation.

Another example:

let x = 0.3 - 0.2;
console.log(x); // 0.09999999999999998

As you can see, the result of 0.3 - 0.2 should be 0.1 but due to the way that JavaScript stores floating-point numbers, it is not exactly 0.1 but a very close approximation.

To avoid this problem, you can use the libraries we talked about earlier, which provide a more precise decimal arithmetic.

The + Operator (Addition and Concatenation)

In JavaScript, the + operator can be used for both addition and concatenation, depending on the types of the operands.

If both operands are numbers, the + operator performs addition and returns the sum of the operands.

For example:

let x = 3;
let y = 4;
let z = x + y;
console.log(z); // 7

If one or both operands are strings, the + operator performs concatenation and returns a new string that concatenates the two operands.

For example:

let x = "Hello";
let y = "World";
let z = x + " " + y;
console.log(z); // "Hello World"

If one of the operands is a string, JavaScript converts the other operand to a string and performs concatenation.

For example:

let x = "The result is: ";
let y = 3;
let z = x + y;
console.log(z); // "The result is: 3"

Also, if one of the operands is a numeric string, JavaScript converts it to a number and performs addition.

For example:

let x = "3";
let y = 4;
let z = x + y;
console.log(z); // "34"

It's important to be aware that the + operator performs addition and concatenation depending on the types of the operands.

And in case of numeric strings, JavaScript converts them to numbers before performing addition, but if the strings are not numeric, the + operator performs concatenation.

You can use the Number() function or the parseInt() and parseFloat() functions to convert strings to numbers and avoid confusion.

What is NaN in JavaScript

In JavaScript, NaN stands for "Not a Number", and it is a special value that represents an undefined or unrepresentable value. It is a property of the global object and is of type "Number".

NaN is returned when a mathematical operation cannot be performed or when a value that is not a number is used in a mathematical operation.

For example, taking the square root of a negative number or dividing zero by zero will return NaN.

console.log(Math.sqrt(-1)); // NaN
console.log(0/0); // NaN

Also, parsing a non-numeric string using parseInt() or parseFloat() returns NaN.

console.log(parseInt("not a number")); // NaN
console.log(parseFloat("not a number")); // NaN

It's important to note that NaN is not equal to any value, including itself. So, the following comparison will return false

console.log(NaN === NaN); // false

To check if a value is NaN, you can use the isNaN() function or the Number.isNaN() method.

console.log(isNaN(NaN)); // true
console.log(Number.isNaN(NaN)); // true

Also, JavaScript provides a isFinite() function to determine whether a value is a finite number, which includes all numbers, including infinity and negative infinity, but excludes NaN.

console.log(isFinite(NaN)); // false

It's important to be aware of NaN and to handle it properly in your code to avoid unexpected behavior or errors.

What is Infinity in JavaScript

In JavaScript, Infinity is a special value that represents a number that is greater than the maximum representable number. It is a property of the global object and is of type "Number".

The value of infinity is represented by the constant Infinity. JavaScript also has a negative infinity represented by the constant -Infinity.

For example, the following mathematical operations return infinity:

console.log(1 / 0); // Infinity
console.log(-1 / 0); // -Infinity

You can also use the Number.POSITIVE_INFINITY and Number.NEGATIVE_INFINITY constants to represent infinity and negative infinity respectively.

console.log(Number.POSITIVE_INFINITY); // Infinity
console.log(Number.NEGATIVE_INFINITY); // -Infinity

It's important to note that infinity is not equal to any value, including itself, and any mathematical operation with infinity returns infinity, except for 0 * infinity which returns NaN.

console.log(Infinity === Infinity); // false
console.log(Infinity + Infinity); // Infinity
console.log(0 * Infinity); // NaN

JavaScript also provides a isFinite() function to determine whether a value is a finite number, which includes all numbers, including infinity and negative infinity, but excludes NaN.

console.log(isFinite(Infinity)); // false
console.log(isFinite(-Infinity)); // false

It's important to be aware of Infinity and to handle it properly in your code to avoid unexpected behavior or errors.

JS Basics